Welcome to Subscribe On Youtube
45. Jump Game II
Description
You are given a 0-indexed array of integers nums
of length n
. You are initially positioned at nums[0]
.
Each element nums[i]
represents the maximum length of a forward jump from index i
. In other words, if you are at nums[i]
, you can jump to any nums[i + j]
where:
0 <= j <= nums[i]
andi + j < n
Return the minimum number of jumps to reach nums[n - 1]
. The test cases are generated such that you can reach nums[n - 1]
.
Example 1:
Input: nums = [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.
Example 2:
Input: nums = [2,3,0,1,4] Output: 2
Constraints:
1 <= nums.length <= 104
0 <= nums[i] <= 1000
- It's guaranteed that you can reach
nums[n - 1]
.
Solutions
Solution 1: Greedy Algorithm
We can use a variable $mx$ to record the farthest position that can be reached from the current position, a variable $last$ to record the position of the last jump, and a variable $ans$ to record the number of jumps.
Next, we traverse each position $i$ in $[0,..n - 2]$. For each position $i$, we can calculate the farthest position that can be reached from the current position through $i + nums[i]$. We use $mx$ to record this farthest position, that is, $mx = max(mx, i + nums[i])$. Then, we check whether the current position has reached the boundary of the last jump, that is, $i = last$. If it has reached, then we need to make a jump, update $last$ to $mx$, and increase the number of jumps $ans$ by $1$.
Finally, we return the number of jumps $ans$.
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
-
class Solution { public int jump(int[] nums) { int ans = 0, mx = 0, last = 0; for (int i = 0; i < nums.length - 1; ++i) { mx = Math.max(mx, i + nums[i]); if (last == i) { ++ans; last = mx; } } return ans; } }
-
class Solution { public: int jump(vector<int>& nums) { int ans = 0, mx = 0, last = 0; for (int i = 0; i < nums.size() - 1; ++i) { mx = max(mx, i + nums[i]); if (last == i) { ++ans; last = mx; } } return ans; } };
-
class Solution: def jump(self, nums: List[int]) -> int: current_reach = next_reach = steps = 0 # stop at 2nd-to-last, not the last index. # because, eg. nums=[1,0] or nums=[0,0], # just check the 2nd-to-last then we can decide if able to reach end # eg. - if input is [0], then 0 step needed for i, num in enumerate(nums[:-1]): next_reach = max(next_reach, i + num) if i == current_reach: current_reach = next_reach # update next-reach before if check steps += 1 # in question, guarenteed can reach end. or else need more check return steps ############ class Solution(object): def jump(self, nums): """ :type nums: List[int] :rtype: int """ pos = 0 ans = 0 bound = len(nums) while pos < len(nums) - 1: dis = nums[pos] farthest = posToFarthest = 0 for i in range(pos + 1, min(pos + dis + 1, bound)): canReach = i + nums[i] if i == len(nums) - 1: return ans + 1 if canReach > farthest: farthest = canReach posToFarthest = i ans += 1 pos = posToFarthest return ans
-
func jump(nums []int) (ans int) { mx, last := 0, 0 for i, x := range nums[:len(nums)-1] { mx = max(mx, i+x) if last == i { ans++ last = mx } } return }
-
function jump(nums: number[]): number { let [ans, mx, last] = [0, 0, 0]; for (let i = 0; i < nums.length - 1; ++i) { mx = Math.max(mx, i + nums[i]); if (last === i) { ++ans; last = mx; } } return ans; }
-
public class Solution { public int Jump(int[] nums) { int ans = 0, mx = 0, last = 0; for (int i = 0; i < nums.Length - 1; ++i) { mx = Math.Max(mx, i + nums[i]); if (last == i) { ++ans; last = mx; } } return ans; } }
-
impl Solution { pub fn jump(nums: Vec<i32>) -> i32 { let n = nums.len(); let mut dp = vec![i32::MAX; n]; dp[0] = 0; for i in 0..n - 1 { for j in 1..=nums[i] as usize { if i + j >= n { break; } dp[i + j] = dp[i + j].min(dp[i] + 1); } } dp[n - 1] } }
-
<?php class Solution { /** * @param integer[] $nums * @return integer */ function jump($nums) { $maxReach = 0; $steps = 0; $lastJump = 0; for ($i = 0; $i <= count($nums) - 2; $i++) { $maxReach = max($maxReach, $i + $nums[$i]); if ($i == $lastJump) { $lastJump = $maxReach; $steps++; } } return $steps; } }