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
相关推荐
_深海凉_7 小时前
LeetCode热题100-最小栈
java·数据结构·leetcode
不知名的忻7 小时前
Morris遍历(力扣第99题)
java·算法·leetcode·morris遍历
_深海凉_8 小时前
LeetCode热题100-除了自身以外数组的乘积
数据结构·算法·leetcode
米粒110 小时前
力扣算法刷题 Day 42(股票问题总结)
算法·leetcode·职场和发展
浅念-12 小时前
从LeetCode入门位运算:常见技巧与实战题目全解析
数据结构·数据库·c++·笔记·算法·leetcode·牛客
田梓燊12 小时前
leetcode 142
android·java·leetcode
_深海凉_12 小时前
LeetCode热题100-最大数(179)
算法·leetcode·职场和发展
剑挑星河月13 小时前
763.划分字母区间
数据结构·算法·leetcode
小辉同志13 小时前
74. 搜索二维矩阵
c++·leetcode·矩阵·二分查找
圣保罗的大教堂13 小时前
leetcode 3740. 三个相等元素之间的最小距离 I 简单
leetcode