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
相关推荐
YGGP1 小时前
【Golang】LeetCode 32. 最长有效括号
算法·leetcode
YGGP4 小时前
【Golang】LeetCode 5. 最长回文子串
算法·leetcode
历程里程碑5 小时前
滑动窗口解法:无重复字符最长子串
数据结构·c++·算法·leetcode·职场和发展·eclipse·哈希算法
youngee116 小时前
hot100-55有效的括号
算法·leetcode·职场和发展
YGGP8 小时前
【Golang】LeetCode 72. 编辑距离
算法·leetcode
YGGP9 小时前
【Golang】LeetCode 62. 不同路径
算法·leetcode
努力学算法的蒟蒻9 小时前
day47(12.28)——leetcode面试经典150
算法·leetcode·面试
iAkuya11 小时前
(leetcode)力扣100 26环状链表2(双指针)
算法·leetcode·链表
sin_hielo11 小时前
leetcode 2402(双堆模拟,小根堆)
数据结构·算法·leetcode
Morwit11 小时前
【力扣hot100】 312. 戳气球(区间dp)
c++·算法·leetcode