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
相关推荐
SylviaW084 小时前
python-leetcode 37.翻转二叉树
算法·leetcode·职场和发展
玦尘、4 小时前
位运算实用技巧与LeetCode实战
算法·leetcode·位操作
夏末秋也凉5 小时前
力扣-回溯-93 复原IP地址
算法·leetcode
01_8 小时前
力扣hot100 ——和为k的子数组 前后缀和(积)各种情况总结
数据结构·算法·leetcode·前后缀和(积)计算
_Itachi__8 小时前
LeetCode 热题 100 206. 反转链表
算法·leetcode·链表
南宫生9 小时前
力扣每日一题【算法学习day.130】
java·学习·算法·leetcode
柠石榴9 小时前
【练习】【类似于子集问题】力扣491. 非递减子序列/递增子序列
c++·算法·leetcode·回溯
干饭高手9 小时前
Day9,Hot100(图论)
python·leetcode·图论
sjsjs119 小时前
【数据结构-并查集】力扣1722. 执行交换操作后的最小汉明距离
数据结构·算法·leetcode
且听风吟ayan10 小时前
leetcode day20 滑动窗口209+904
算法·leetcode·c#