forked from DevOgabek/LeetCodePythonSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbag_of_tokens.py
More file actions
28 lines (24 loc) · 699 Bytes
/
bag_of_tokens.py
File metadata and controls
28 lines (24 loc) · 699 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Bag of Tokens
class Solution(object):
def bagOfTokensScore(self, tokens, power):
tokens.sort()
n = len(tokens)
score = 0
max_score = 0
left = 0
right = n - 1
while left <= right:
if power >= tokens[left]:
power -= tokens[left]
score += 1
left += 1
max_score = max(max_score, score)
elif score > 0:
power += tokens[right]
score -= 1
right -= 1
else:
break
return max_score
solution = Solution()
print(solution.bagOfTokensScore([100], 50))