Bag of Tokens LeetCode solution in Python

1
class Solution:
2
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
3
tokens.sort()
4
i, j = 0, len(tokens) - 1
5
m = score = 0
6
while i < j:
7
if tokens[i] > power and score == 0:
8
break
9
if tokens[i] <= power:
10
score += 1
11
power -= tokens[i]
12
m = max(m, score)
13
i += 1
14
else:
15
if score > 0:
16
score -= 1
17
power += tokens[j]
18
j -= 1
19
if i < len(tokens) and tokens[i] <= power:
20
score += 1
21
m = max(m, score)
22
return m

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0