代码随想录算法训练营第32天| 贪心

发布时间:2024年01月24日

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 = end
    new_start = 0
    for i in range(start, end):
        if nums[i]+i+1 >= cur:
            cur = nums[i]+i+1
            new_start = i
    end = cur
    start = new_start+1
    res += 1
return res

文章来源:https://blog.csdn.net/qq_60843366/article/details/135775021
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。