[Leetcode] Jump Game II Jumping games two

Source: Internet
Author: User
Tags jumping games

Given an array of non-negative integers, you is initially positioned at the first index of the array.

Each element of the array represents your maximum jump length is 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 and then steps to the last 3 index.)

This is the jump game before the extension, the question is to ask can reach the last number, and this problem only let us get to the last position of the minimum number of hops, it seems that the default must be able to reach the final position? The core method of the problem is to use the dynamic programming of the programming idea to solve, we need two variables cur and pre respectively to save the current can reach the farthest position and the furthest position can reach, as long as the cur did not reach the last position, the loop continues, The pre records the value of cur, and if the current position I is less than or equal to the pre, then update cur and then I increment by 1. The method of updating Cur is to compare the larger values in the current cur and i + a[i], and so on when I loop to the pre value, the number of hops plus one, if the title does not indicate whether the end can be reached, we can also determine whether the pre and cur are equal, if the equality of the cur is not updated, That is, you cannot reach the end position, return-1, the code is as follows:

Solution One

classSolution { Public:    intJumpintA[],intN) {intres =0, i =0, cur =0;  while(Cur < n-1) {            intPre =cur;  while(I <=pre) {cur= Max (cur, i +A[i]); ++i; }            ++Res; if(pre = = cur)return-1;//May isn't need this        }        returnRes; }};

There is a way of writing, with the above solution slightly different, but the essence of the idea is the same, the detailed analysis of this solution can be found in the lab small paper stickers off-campus blog, the code is as follows:

Solution Two

classSolution { Public:    intJumpintA[],intN) {intres =0, last =0, cur =0;  for(inti =0; I < n-1; ++i) {if(I >Last ) { Last=cur; ++Res; if(cur >= N-1) Break; } cur= Max (cur, i+A[i]); }        returnRes; }};

[Leetcode] Jump Game II Jumping games two

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.