【LeetCode每天一题】Jump Game II(跳跃游戏II)

Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.Jump 1 step from index 0 to 1, then 3 steps to the last index.

Note:You can assume that you can always reach the last index.

思路



这个我们可以参考跳跃游戏I的那个接替思路,只不过这里我们需要增加一个下标变量来记录step数。其余的都和跳跃游戏I一样。时间复杂度为O(n),空间复杂度为O(1)。

图示步骤



解决代码


 1 class Solution(object):
 2     def jump(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: int
 6         """
 7         max_end = 0
 8         end = 0
 9         step = 0            # 记录步数
10         for i in range(len(nums)-1):
11             max_end = max(max_end, nums[i]+i)
12             if i == end:       # 跟新最大下标位置,并步数加一
13                 end = max_end
14                 step += 1
15         return step

原文地址:https://www.cnblogs.com/GoodRnne/p/10734897.html

时间: 2024-08-27 21:01:29

【LeetCode每天一题】Jump Game II(跳跃游戏II)的相关文章

LeetCode(45): 跳跃游戏 II

Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2.   从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置. 说明: 假设你总是可以到达数组的最后一个位置. 解题思路: 这题是之前那道Jump Game 跳跃游戏 的延伸,那题是问能不能

跳跃游戏 II

题目描述:给出一个非负整数数组,你最初定位在数组的第一个位置.数组中的每个元素代表你在那个位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 样例:给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次) 跟上一道题:"跳跃游戏"(详见:点击打开链接)基本逻辑是一样的,都是贪心法的应用.其实这道题当中,贪心法用得更明显了. 为了能够以最少的步数跳到最后,则每次

leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum nu

LeetCode 55. Jump Game (跳跃游戏)

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A = 

LeetCode OJ:Jump Game(跳跃游戏)

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A = 

[Swift]LeetCode45. 跳跃游戏 II | Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

#leetcode刷题之路45-跳跃游戏 II

给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例:输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2. 从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置.说明:假设你总是可以到达数组的最后一个位置. 原文地址:https://www.cnblogs.com/biat/p/10664843.html

leetcode——45. 跳跃游戏 II

我真的是超开心了,又做对了!!!!!!而且没走啥弯路!!!!!!! class Solution(object): def jump(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums)<2: return 0 pact=0 i=0 while i<len(nums): if nums[i]>=len(nums)-i-1: pact+=1 retu

leetcode-45.跳跃游戏II(hard)

给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2.  从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置.说明: 假设你总是可以到达数组的最后一个位置. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/jump