leetcode hot 100 跳跃游戏2

45. 跳跃游戏 II

已解答

中等

相关标签

相关企业

给定一个长度为 n0 索引 整数数组 nums。初始位置为 nums[0]

每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i + j] 处:

  • 0 <= j <= nums[i]
  • i + j < n

返回到达 nums[n - 1] 的最小跳跃次数。生成的测试用例可以到达 nums[n - 1]

复制代码
class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """

        max_t = 1
        count=0
        queue = [0]
        queue_next=[]
        while max_t<len(nums):
            for tmp in queue:
                if tmp + nums[tmp]+1>max_t:
                    end = min(len(nums),tmp+nums[tmp]+1)
                    for i in range(max_t,end):
                        
                        queue_next.append(i)
                    max_t = end
            count+=1
            queue = queue_next
            queue_next=[]

保存一个队列,是上一次能到达的最远距离

相关推荐
KingRumn1 天前
Linux信号之标准信号与实时信号
linux·算法
源代码•宸1 天前
Leetcode—620. 有趣的电影&&Q3. 有趣的电影【简单】
数据库·后端·mysql·算法·leetcode·职场和发展
2301_800256111 天前
地理空间数据库中的CPU 和 I/O 开销
数据库·算法·oracle
腾讯WeTest1 天前
范式转移:LLM如何重塑游戏自动化测试的底层逻辑
功能测试·游戏·ai·腾讯wetest
一个不知名程序员www1 天前
算法学习入门---结构体和类(C++)
c++·算法
XFF不秃头1 天前
力扣刷题笔记-旋转图像
c++·笔记·算法·leetcode
王老师青少年编程1 天前
csp信奥赛C++标准模板库STL案例应用3
c++·算法·stl·csp·信奥赛·lower_bound·标准模版库
云水木石1 天前
Android 的下一个战场:Windows 应用与游戏?
android·windows·游戏
有为少年1 天前
Welford 算法 | 优雅地计算海量数据的均值与方差
人工智能·深度学习·神经网络·学习·算法·机器学习·均值算法