Leetcode Jumping game series

Source: Internet
Author: User

55. Jumping Games

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:

Input: [3,2,1,0,4] Output: false explanation: Anyway, you always get to the position of index 3. But the maximum jump length for this location is 0, so you can never reach the last position.

Intuitively, because the problem is just asking if you can jump to the last position, you can jump as far as possible in total. Remember longest is the farthest position that can be reached, then there is longest = max (longest, i + nums[i]). If jumping from the starting point to longest can not reach the current position, then the last position can not be reached.

classSolution { Public:    BOOLCanjump (vector<int>&nums) {        intLongest =0;  for(inti =0; I < nums.size (); i++) {            if(Longest <i) {return false; } Longest= Max (Longest, i +Nums[i]); }        returnLongest >= nums.size ()-1; }};
45. Jumping Game II

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. Your goal is to use a minimum number of hops to reach the last position of the array.

Example:

2。 Jump    from subscript 0 to the position labeled 1, skip 1 step, and jump to 3 the last position of the array.

Description

Suppose you can always reach the last position of the array.

Because the problem is to find the minimum number of steps, the first reaction is to solve by dynamic programming. Set the array longest and DP, where Longest[i] represents the farthest place to go from I, dp[i] represents the minimum number of steps required to reach I. Then dp[i] = min (Dp[j] + 1, dp[i]), where DP is initialized to int_max,dp[0] = 0, transfer condition is J < i && dp[j]! = Int_max && Longest[j] >= i.

classSolution { Public:    intJump (vector<int>&nums) {        intTMP =0; Vector<int>DP (Nums.size (), Int_max); Vector<int> Longest (Nums.size (),0); dp[0] =0;  for(inti =0; I < nums.size (); i++) {Longest[i]= TMP = MAX (TMP, Nums[i] +i); }         for(inti =1; I < nums.size (); i++) {             for(intj =0; J < I; J + +) {                if(Dp[j]! = Int_max && Longest[j] >=i) {Dp[i]= Min (Dp[i], dp[j] +1); }            }        }        returnDp[nums.size ()-1]; }};

But doing this will time out. Because the problem is the minimum number of steps, you can consider the BFS approach. Think of each index as a node that can be viewed at each level. First on the code:

intJumpintA[],intN) {if(N&LT;2)return0; intLevel=0,currentmax=0,i=0,nextmax=0;  while(currentmax-i+1>0) {//nodes Count of current level>0level++;  for(; i<=currentmax;i++) {//traverse current level, and update the Max reach of next levelNextmax=max (nextmax,a[i]+i); if(nextmax>=n-1)returnLevel//if last element was in level+1 and then the Min jump=level} Currentmax=Nextmax; }     return0; }

Level represents the number of layers of the layer node and the number of steps to the next level . Currentmax represents the farthest reachable node index in the current layer, and Nextmax is used to represent the index of the node farthest to the next level.

This BFS is not explicitly used in queues and needs to be learned.

Leetcode Jumping game series

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.