# 【Leetcode】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.

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

class Solution {
public:
int jump(int A[], int n) {
return Jump(A,n,0);
}

int Jump(int a[],int n,int pos)
{
if(pos>=n-1)
return 0;
int count=1;
int m=a[pos];
if(m==0)
return n;
int min=n;
for(int i=m;i>=1;i--)
{
int temp=Jump(a,n,pos+i);
if(temp<min)
min=temp;
}
count+=min;
return count;
}
};

class Solution {
public:
int jump(int A[], int n) {
int start=0;
int end=0;
int count=0;
if(n==1)
return 0;
while(end<n)
{
count++;
int max=0;
for(int i=start;i<=end;i++)
{
if(A[i]+i>=n-1)
return count;
if(A[i]+i>max)
max=A[i]+i;
}
start=end+1;
end=max;
}
}
};

举报原因： 您举报文章：【Leetcode】Jump Game II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)