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
相关推荐
0+11118 小时前
算法 --模拟
c++·算法·leetcode
Nil20819 小时前
leetcode 739每日温度
算法·leetcode·职场和发展
xqqxqxxq20 小时前
LeetCode Hot100 矩阵专题题解笔记
笔记·leetcode·矩阵
All for pursuit.20 小时前
【贪心-4】581.最短无序连续子数组
数据结构·c++·算法·leetcode
mmmmath_321 小时前
LeetCode.020.有效的括号
数据结构·算法·leetcode
All for pursuit.1 天前
【回溯-2】22.括号生成
数据结构·c++·算法·leetcode
longlongzihan1 天前
LeetCode 1004.最大连续1的个数III 滑动窗口及动态规划题解
c++·算法·leetcode·dp·滑动窗口
Dream it possible!1 天前
LeetCode_热题100_知识点总结
算法·leetcode·职场和发展
蛋蛋的就会顺顺的2 天前
hot100——矩阵
java·数据结构·算法·leetcode·力扣
想吃火锅10052 天前
【leetcode】238.除了自身以外数组的乘积js
javascript·算法·leetcode