代码随想录算法训练营第32天| 贪心
LeetCode.122 买卖股票的最佳时机II
res = 0
for i in range(1, len(prices)):diff = prices[i] - prices[i-1]if diff > 0:res += diff
return res
LeetCode.55 跳跃游戏
cur, end = 0, 0
while end < len(nums) and cur <= end:end = max(end, cur+nums[cur])cur += 1
return end >= len(nums)-1
LeetCode.45 跳跃游戏II
写题的时候逻辑都理不清了,感觉要去放松一下
if len(nums) == 1: return 0
end = 1
start = 0
res = 0
while end < len(nums):cur = endnew_start = 0for i in range(start, end):if nums[i]+i+1 >= cur:cur = nums[i]+i+1new_start = iend = curstart = new_start+1res += 1
return res