-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathbag-of-tokens.py
62 lines (60 loc) · 1.56 KB
/
bag-of-tokens.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/84498305
# IDEA : GREEDY
class Solution(object):
def bagOfTokensScore(self, tokens, P):
"""
:type tokens: List[int]
:type P: int
:rtype: int
"""
print(tokens)
tokens.sort()
N = len(tokens)
left, right = 0, N - 1
points = 0
remain = N
while left < N and P >= tokens[left]:
P -= tokens[left]
points += 1
left += 1
remain -= 1
if left == 0 or left == N: return points
while points > 0 and remain > 1:
P += tokens[right]
right -= 1
points -= 1
remain -= 1
while left <= right and P >= tokens[left]:
P -= tokens[left]
points += 1
left += 1
remain -= 1
return points
# V2
# Time: O(nlogn)
# Space: O(1)
class Solution(object):
def bagOfTokensScore(self, tokens, P):
"""
:type tokens: List[int]
:type P: int
:rtype: int
"""
tokens.sort()
result, points = 0, 0
left, right = 0, len(tokens)-1
while left <= right:
if P >= tokens[left]:
P -= tokens[left]
left += 1
points += 1
result = max(result, points)
elif points > 0:
points -= 1
P += tokens[right]
right -= 1
else:
break
return result