Given a non-negative integer array, you are initially in the first position of the array.
Each element in the array represents the maximum length that you can jump in that position.
Determine if you can reach the last position.
Example 1:
Input: [2,3,1,1,4] Output: True explanation: Jump 1 steps from position 0 to 1, then jump 3 steps to reach the last position.
Example 2:
classSolution { Public: //attention is the maximum jump BOOLCanjump (vector<int>&nums) { intn=nums.size (); Vector<int> DP (N,0); for(inti =1; I < n; ++i) {Dp[i]= Max (Dp[i-1]-1, Nums[i-1]);//Dp[i] Indicates the maximum number of steps left to go to the i-1 position (still available). //For example 1, when you go to a[1]=3, the maximum number of steps left after you reach 3 is if(Dp[i] <=0)return false; } returnDp[n-1] >=0;//go to the maximum number of steps left}};
Leetcode#55 Jump Game