# 45. Jump Game II

## Description

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 1:

``````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.

Tags: Math, String

## 题解

### 思路1

DP 基本上会超时

``````func jump(nums []int) int {
n := len(nums)
if n < 1 {
return 0
}

dp := make([]int, n)
for i := 0; i < len(nums); i++ {
dp[i] = math.MaxInt32
}
dp[0] = 0

for i := 0; i < n-1; i++ {
for j := 1; j <= nums[i]; j++ {
if i+j < n {
dp[i+j] = Min(dp[i+j], dp[i]+1)
}
}
}
return dp[n-1]
}
``````

### 思路2

``````func jump(nums []int) int {
//特殊情况判断
if len(nums)==1{
return 0
}else if nums[0] >= len(nums){
return 1
}
left, right, res := 0, nums[0], 0
for right < len(nums)-1 {
max := 0
for i := left; i <= right; i++ {
//寻找最大跨度
if nums[i]-(right-i) >= max{
max = nums[i]-(right - i)
}
}
//窗口滑动右滑
left = right
right += max
res++
}
//判断最后一步是不是踏在最后一个格子。循环条件结束只能确定能到达边界，left指针才是每次跳的格子。
if left<len(nums)-1{
res++
}
return res
}
``````