[Leetcode problem Solving record] Jump game and jump game II

Source: Internet
Author: User

Jump Game

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.

Determine if you is able to reach the last index.

For example:
A = [2,3,1,1,4] , return true .

A = [3,2,1,0,4] , return false .

From the title meaning is given to a nonnegative integer array, where your initial position is in the first element, and the value of each element represents the maximum distance that the position can jump. Use the algorithm to determine if you can reach the last element.

The hint tag is an array and greedy (greedy), but this should be the dynamic programming solution, because the greedy algorithm needs to be guaranteed to have the solution, here still need to judge, and cannot guarantee.

We use maxposition to maintain a farthest position that can be reached from the start position, and then determine if the current position can be in the final position and whether the current position is reachable, if two conditions are satisfied, then return true if the current position is 0, and the farthest position cannot exceed the current position, Then you can only return false, updating the farthest position. The Java code is as follows:

     Public BooleanCanjump (int[] A) {        if(A.length <= 1)            return true; if(A[0] >= (a.length-1))            return true; intMaxposition = a[0]; if(Maxposition = = 0)            return false;  for(inti = 1; i < a.length-1; i++){            if(Maxposition >= i && (i + a[i]) >= a.length-1)                return true; if(Maxposition <= i && a[i] = = 0)                return false; if(Maxposition < (i +A[i])) Maxposition= i +A[i]; }        return false; }

Or you can write that.

     Public BooleanCanjump (int[] A) {        intmaxposition = 0;  for(intstart = 0; Start <= maxposition && start < a.length; Start + +){            if((A[start] + start) >maxposition) maxposition= (A[start] +start); if(Maxposition >= (a.length-1))return true; }        return false; }

Jump Game II

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

The title means the minimum number of hops to reach the last element. Using greedy algorithm, the solution is as follows (Java), can pass. But what if there's no solution? Or the greedy method can not find the solution?

     Public intJumpint[] A) {        intMaxx=0,temp=0,count=0;  for(inti = 0; I <a.length;) {if(Temp >= (a.length-1)) Break;  while(I <=temp) {Maxx= maxx> (i + a[i])? Maxx: (i +A[i]); I++; } Count++; Temp=Maxx; }        returncount; }

[Leetcode problem Solving record] Jump game and jump game II

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.