Leetcode#55 Jump Game

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.