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
相关推荐
剑挑星河月28 分钟前
234. 回文链表
java·数据结构·算法·leetcode·链表
硕风和炜12 小时前
【LeetCode: 2492. 两个城市间路径的最小分数 + DFS】
java·算法·leetcode·深度优先·dfs·bfs·并查集
凯瑟琳.奥古斯特14 小时前
K次取反最大化数组和解法(力扣1005)
开发语言·c++·算法·leetcode·职场和发展
tachibana216 小时前
hot100 回文链表(234)
java·网络·数据结构·leetcode·链表
wabs66619 小时前
关于动态规划【力扣1143.最长公共子序列的思考】
算法·leetcode·动态规划
剑挑星河月19 小时前
54.螺旋矩阵
java·算法·leetcode·矩阵
笨笨没好名字21 小时前
Leetcode刷题python3版第一周(下)
linux·算法·leetcode
青山木1 天前
Hot 100 --- LRU 缓存
java·数据结构·算法·leetcode·链表·缓存·哈希
想你依然心痛1 天前
AtomCode在算法竞赛中的实战体验:LeetCode周赛辅助编程
linux·算法·leetcode
剑挑星河月1 天前
35.搜索插入位置
java·数据结构·算法·leetcode