Skip to content

Commit fadf450

Browse files
authored
Create 0948-bag-of-tokens.py
1 parent 54103c8 commit fadf450

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

python/0948-bag-of-tokens.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
3+
res = score = 0
4+
tokens.sort()
5+
6+
l, r = 0, len(tokens) - 1
7+
while (l <= r):
8+
if power >= tokens[l]:
9+
power -= tokens[l]
10+
l += 1
11+
score += 1
12+
res = max(res, score)
13+
elif score > 0:
14+
power += tokens[r]
15+
r -= 1
16+
score -= 1
17+
else:
18+
break
19+
return res

0 commit comments

Comments
 (0)