[Leetcode] [Java] Jump Game II

Source: Internet
Author: User

Title:

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


Test Instructions:


Given a non-negative integer array. The starting position of the given initialization position in the array.

Each element in the array represents the maximum distance that you can jump in this position.

Your goal is to reach the end of the array with a minimum number of hops.

For example: given A =[2,3,1,1,4]

The minimum number of jump steps to reach the end of the array is 2. (1 steps from index 0 to 1, followed by 3 steps to reach the end index.) )


Algorithm Analysis:

The main idea of the problem is. Scans the array to determine the node that is currently farthest from being overwritten. Put Maxreach.

Then proceed with the scan until the current distance exceeds reach of the last calculated coverage, then update the coverage and update the number of bars at the same time, as we have been able to move forward with more than one jump.

Figuratively speaking, this is in the fight for every jump furthest greedy.

* RET: Number of jump up to now


* Currch: The maximum range reached after a RET jump from a[0]


* Curmax: The maximum range that can be reached from 0~i this i+1 a element

* When Currch < I, the RET jump is not enough to cover the current element I. So we need to add a jump to make it reach


* Record the Curmax.



AC Code:

/**    * RET: The jump number so far    * Currch: The maximum range reached after a RET jump from a[0]    * Curmax: The maximum range that can be reached from 0~i this i+1 a element        * when Currch < I, stating that the RET jump has not been enough to cover the current element I, so I need to add one. To make it up    to the Curmax of the record.

*/public class Solution {public int jump (int[] nums) { int ret = 0; int curmax = 0; int currch = 0; for (int i = 0; i < nums.length; i + +) { if (Currch < i) { ret + +; Currch = Curmax; } Curmax = Math.max (Curmax, nums[i]+i); } return ret; } }




[Leetcode] [Java] 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.