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
相关推荐
java_nnnn5 小时前
LeetCode刷题滑动窗口(串联所有单词的子串+最小覆盖的子串)Java
算法·leetcode·职场和发展
Navigator_Z6 小时前
LeetCode //C - 1301. Number of Paths with Max Score
c语言·算法·leetcode
Nuanyt8 小时前
LeetCode Hot100 刷题笔记 · 普通数组专题
java·笔记·算法·leetcode
春弦_10 小时前
LeetCode Hot 100 刷题笔记
笔记·leetcode·哈希算法
凯瑟琳.奥古斯特13 小时前
力扣1015C++解法技巧
c++·算法·leetcode·链表·职场和发展
文丑颜不良啊13 小时前
LeetCode626~650题目解析
java·leetcode
文丑颜不良啊14 小时前
LeetCode601~625题目解析
java·leetcode
文丑颜不良啊14 小时前
LeetCode651~675题目解析
java·leetcode
Xin77015 小时前
LeetCode 78. 子集(回溯法)
leetcode
INGNIGHT15 小时前
151 · 买卖股票的最佳时机 III(序列dp)
数据结构·算法·leetcode