Skip to content

Commit 128eec2

Browse files
authored
Create 948. Bag of Tokens.py
1 parent 915adea commit 128eec2

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
6+
tokens.sort() #Olog(n)
7+
left = 0 #가장 작은 토큰
8+
right = len(tokens)-1 #가장 큰 토큰
9+
score = 0
10+
max_score = 0
11+
while left <=right:#O(n)
12+
if power >=tokens[left]:#face-up
13+
power -= tokens[left]
14+
score += 1
15+
left += 1
16+
max_score = max(max_score,score)
17+
elif score > 0 and left < right:#face-down
18+
power += tokens[right]
19+
score -= 1
20+
right -= 1
21+
else:
22+
break
23+
return max_score
24+
25+
26+
tokens = [100,200,300,400]
27+
power = 200
28+
solution = Solution()
29+
print(solution.bagOfTokensScore(tokens,power))

0 commit comments

Comments
 (0)