Jump Game | & | |

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.

Example

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

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

Analysis:

Use a variable to save the currently remaining "steps to jump", if it is less than or equal to 0, it means that you cannot jump further down.

1  Public classSolution {2     /**3 * @param a:a list of integers4 * @return: The Boolean answer5      */6      PublicBoolean Canjump (int[] nums) {7         if(Nums = =NULL)return false;8         intStepsremaining =1;9         Ten          for(inti =0; i < nums.length; i++) { Onestepsremaining = Math.max (Stepsremaining-1, Nums[i]); A             if(Stepsremaining <=0&& I! = nums.length-1)return false; -         } -         return true; the     } -}
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.

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

Analysis:

When we are at the first point "2", we need to traverse all reachable points, i.e., 3, 1, and then find out at which point (3 or 1) the number of hops left is the highest when we reach the 3rd point.

1  Public classSolution {2     /**3 * @param a:a List of lists of integers4 * @return: An integer5      */6      Public intJumpint[] A) {7         if(A = =NULL|| A.length = =0)return 0;8 9         intPosition =0, Totaljumps =0;Ten         intRemainingjumps =0; One         intjumps = a[0] +1;//assume we jumps in the array at the beginning A  -          while(Position <a.length) { -totaljumps++; the             //find Max remaining jumps in the ' Jump Range ' -              for(inti =1; I <= jumps; i++) { -Remainingjumps = Math.max (A[position], remainingjumps-1); -position++; +                 if(Position >= a.length) Break; -             } +jumps =Remainingjumps; A         } at         returnTotaljumps; -     }  -}

Reference Please specify Source: cnblogs.com/beiyeqingteng/

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.