-
Notifications
You must be signed in to change notification settings - Fork 0
/
948.py
30 lines (27 loc) · 791 Bytes
/
948.py
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
29
30
class Solution(object):
def bagOfTokensScore(self, tokens, P):
"""
:type tokens: List[int]
:type P: int
:rtype: int
"""
tokens = sorted(tokens)
point = 0
max_point = 0
start = 0
end = len(tokens) - 1
if len(tokens) == 1:
return 1 if tokens[0] < P else 0
while start < end and point >= 0:
while P >= tokens[start]:
point += 1
P -= tokens[start]
start += 1
while P < tokens[start]:
max_point = max(max_point, point)
point -= 1
P += tokens[end]
end -= 1
return max_point
solu = Solution()
print solu.bagOfTokensScore([71, 55, 82], 54)