LEETCODE-DAY32


title: LEETCODE-DAY32

date: 2024-03-24 13:32:03
tags:

122.买卖股票的最佳时机II、55. 跳跃游戏、45.跳跃游戏II

T1

python 复制代码
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        result = 0
        for i in range(1, len(prices)):
            result += max(prices[i] - prices[i - 1], 0)
        return result

T2

python 复制代码
class Solution:
    def canJump(self, nums: List[int]) -> bool:

        cover = 0
        if len(nums) == 1: return True
        for i in range(0, len(nums)):
            if i <= cover:
                cover = max(i + nums[i], cover)
                # print(cover)
                if cover >= len(nums)-1:
                    return True
        return False
python 复制代码

T3

python 复制代码
class Solution:
    def jump(self, nums):
        cur_distance = 0  # 当前覆盖的最远距离下标
        ans = 0  # 记录走的最大步数
        next_distance = 0  # 下一步覆盖的最远距离下标
        
        for i in range(len(nums) - 1):  # 注意这里是小于len(nums) - 1,这是关键所在
            next_distance = max(nums[i] + i, next_distance)  # 更新下一步覆盖的最远距离下标
            if i == cur_distance:  # 遇到当前覆盖的最远距离下标
                cur_distance = next_distance  # 更新当前覆盖的最远距离下标
                ans += 1
        
        return ans
相关推荐
吃着火锅x唱着歌2 小时前
LeetCode 1128.等价多米诺骨牌对的数量
算法·leetcode·职场和发展
十八岁讨厌编程2 小时前
【算法训练营 · 补充】LeetCode Hot100(中)
算法·leetcode
小当家.1054 小时前
[LeetCode]Hot100系列.贪心总结+思想总结
算法·leetcode·职场和发展
im_AMBER5 小时前
Leetcode 46
c语言·c++·笔记·学习·算法·leetcode
努力学算法的蒟蒻6 小时前
day09(11.6)——leetcode面试经典150
算法·leetcode·职场和发展
好学且牛逼的马7 小时前
【HOT100|1 LeetCode 1. 两数之和】
数据结构·算法·leetcode
ʚ希希ɞ ྀ8 小时前
leeCode hot 100 !!!持续更新中
数据结构·算法·leetcode
剪一朵云爱着8 小时前
力扣1539. 第 k 个缺失的正整数
算法·leetcode
吃着火锅x唱着歌12 小时前
LeetCode 2016.增量元素之间的最大差值
数据结构·算法·leetcode
元亓亓亓15 小时前
LeetCode热题100--46. 全排列--中等
算法·leetcode·职场和发展