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=[]

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

相关推荐
Aloys_Code21 小时前
逆向一个被遗忘的DVD游戏格式:从DES加密到Rust模拟器
游戏·模拟器·retroarch·复古游戏·native32·sunplus·赤刃·钢铁风暴
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
JieE2121 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack202 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
金銀銅鐵2 天前
用 Pygame 实现 15 puzzle
python·数学·游戏
小小杨树2 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2122 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2122 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术3 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像