## 题目

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.

For example:
Given array A = [2,3,1,1,4]

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

## 思路

The ideas is simple ,using some greedy tactics.

We generally maintain a start position to indicate the current position,and there are only 2 main steps for solving the problem.

step(1): When you are at position start, you can go to positon start + 1 to start + nums[start] if nums[start] > 0,
and now you can know whether you can go to the end by judging nums[start] >= nums.size()-1.
If so, go to step(3). If not, go to step(2).

step(2): From start + 1 to start + nums[start], in these nums[start] positions,pick one to be the next start position.
And the rule for picking is simple:

You pick a position start + i, from which you can move forward the longest distance,
which means i + nums[start + i] >= j + nums[start + j] for any 1 <= j <= nums[start].

So the start position is at start + i now. Then you go to the step(1).

step(3): Return the answer.

int count;
void minJump(int *nums,int numsSize,int start){
if(nums==NULL||numsSize<1){
return 0;
}
count++;
int end=start+nums[start];
if(end>=numsSize-1){
return ;
}
int max=0;
int index=0;
for(int i=start+1;i<=end;i++){//寻找在这个区间中，离目标最近的那个点
if(i+nums[i]>max){
index=i;
max=i+nums[i];
}
}
minJump(nums,numsSize,index);//起点换了

}

int jump(int* nums, int numsSize) {
if(nums==NULL||numsSize<1){
return 0;
}
if(numsSize==1){//一个元素
return 0;
}
count=0;
minJump(nums,numsSize,0);
return count;

}

#### 【LeetCode】Jump Game II 解题报告

2014-12-09 17:17:02

#### LeetCode_Jump Game&&Jump Game II

2014-08-09 12:46:38

#### Jump Game II -- LeetCode

2014-03-20 23:25:59

#### LeetCode --- 45. Jump Game II

2015-02-10 08:59:21

#### [leetcode]45. Jump Game II 跳棋游戏2 C++/PYTHON实现【hard难度】

2016-06-17 10:15:08

#### LeetCode | Jump Game II

2013-09-15 21:26:56

#### leetcode解题之45. Jump Game II&55. Jump Game java (跳跃游戏)

2017-04-09 01:09:19

#### Jump Game II (最小步数到达终点，贪心) 【leetcode】

2013-09-20 19:33:17

#### 55. Jump Game Leetcode Python

2015-03-03 06:24:01

#### 45. Jump Game II Leetcode Python

2015-03-03 06:44:55

## 不良信息举报

《leetCode》：Jump Game II