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
相关推荐
Tisfy2 小时前
LeetCode 3536.两个数字的最大乘积:O(1)空间维护max2
数学·算法·leetcode·题解
alphaTao1 天前
LeetCode 每日一题 2026/7/20-2026/7/26
算法·leetcode
圣保罗的大教堂1 天前
leetcode 3514. 不同 XOR 三元组的数目 II 中等
leetcode
青山木1 天前
Hot 100 ---腐烂的橘子
java·数据结构·后端·算法·leetcode·广度优先
xqqxqxxq1 天前
LeetCode Hot100 双指针专项题解笔记
笔记·算法·leetcode
闪电悠米1 天前
力扣hot100-54.螺旋矩阵-模拟边界控制详解
算法·leetcode·矩阵
To_OC2 天前
LC 51 N 皇后:我以为难的是回溯,结果栽在了对角线下标
javascript·算法·leetcode
闪电悠米2 天前
力扣hot100-73.矩阵置零-标记数组详解
算法·leetcode·矩阵
过期动态2 天前
【LeetCode 热题 100】找到字符串中所有字母异位词
java·数据结构·算法·leetcode·职场和发展·rabbitmq
Adios7943 天前
设置交集大小至少为2
数据结构·算法·leetcode