948. Bag of Tokens

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 948

class Solution:
    def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
        tokens.sort()
        i, j = 0, len(tokens) - 1
        res = t = 0
        while i <= j:
            if power >= tokens[i]:
                power -= tokens[i]
                i, t = i + 1, t + 1
                res = max(res, t)
            elif t:
                power += tokens[j]
                j, t = j - 1, t - 1
            else:
                break
        return res