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
相关推荐
小武~9 小时前
Leetcode 每日一题C 语言版 -- 45 jump game ii
c语言·算法·leetcode
leoufung13 小时前
用 DFS 拓扑排序吃透 LeetCode 210:Course Schedule II
算法·leetcode·深度优先
Swift社区14 小时前
LeetCode 443. 压缩字符串
leetcode·职场和发展·蓝桥杯
ada7_14 小时前
LeetCode(python)——543.二叉树的直径
数据结构·python·算法·leetcode·职场和发展
sprintzer14 小时前
11.26-12.05力扣栈刷题
算法·leetcode·职场和发展
sin_hielo14 小时前
leetcode 3578
数据结构·算法·leetcode
前端小白在前进15 小时前
力扣刷题:无重复字符的最长子串
算法·leetcode·职场和发展
好易学·数据结构16 小时前
可视化图解算法72:斐波那契数列
数据结构·算法·leetcode·动态规划·力扣·牛客网
CoderYanger17 小时前
动态规划算法-子数组、子串系列(数组中连续的一段):21.乘积最大子数组
开发语言·算法·leetcode·职场和发展·动态规划·1024程序员节
CoderYanger18 小时前
A.每日一题——3432. 统计元素和差值为偶数的分区方案
java·数据结构·算法·leetcode·1024程序员节