解法
贪心法,存的时候存最小的,减的时候减最大的
先存到不能存,然后加能量,然后再存所有可以存的
注意有可能有时候白换了,所以要统计历史最高得分
class Solution(object):
def bagOfTokensScore(self, tokens, P):
"""
:type tokens: List[int]
:type P: int
:rtype: int
"""
tokens.sort()
now_points = 0
ans = 0
while len(tokens):
while len(tokens) and tokens[0]<=P:
P -= tokens.pop(0)
now_points += 1
ans = max(ans, now_points)
if len(tokens) and now_points:
P += tokens.pop()
now_points -= 1
else:
break
return ans