-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbag.py
More file actions
25 lines (17 loc) · 913 Bytes
/
bag.py
File metadata and controls
25 lines (17 loc) · 913 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
class Solution:
# Logic 1: Make subproblems independent with sorting and solve greedily (ref: sol)
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
tokens.sort() # sorting converts it to greedy removing choice dependency
score = 0
result = 0
while tokens and (power >= tokens[0] or score > 0):
while tokens and tokens[0] <= power: # maximize the score with while to be greedy
curr_card = tokens.pop(0)
power -= curr_card
score += 1
result = max(result, score) # calc max score as we play
if tokens and score > 0: # maximize the power by choosing last element to be greedy
curr_card = tokens.pop()
score -= 1
power += curr_card
return result