Leetcode Jump Game II----java

Source: Internet
Author: User

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 problem is that given an array, and then each number in the array represents the size of the position that can be moved at that position, for example, the first number 2 means that you can move two bits, that is, move to the first 1 (or move to 3), and then 3 represents the maximum can be moved to 4, that is, the last one. Then find the minimum number of steps to move to the last one, in the example 2->3-4 is two steps.

The first idea was to take an array and then find the minimum number of steps to reach each bit, but it timed out.

public class Solution {public    static int jump (int[] nums) {if (nums.length = = 1) return 0;int len = nums.length;int[] R Esult = new Int[len];for (int i = 0; i<len;i++) {if (I+nums[i] >= len-1) {return result[i]+1;} int pos = I;int num = result[i] + 1;for (int j = 0; j<=nums[i];j++,pos++) {if (result[pos] = = 0) result[pos] = num;}} return result[len-1];}}

It seems that the answer should be correct, but the 414ms timeout, which found that the previous algorithm was too violent.

Thus, similar to recursion, the front-to-back traversal, the first I+nums[i encountered] will be the new target location, and will be result+1 until the location POS reaches 0 or 1. The result is 1ms, but it still expires. I don't know what this is.

public class Solution {public    static int jump (int[] nums) {int len = Nums.length;int result = 0, pos = Len-1;while (tru e) {if (  pos = = 0) return result; else if (pos = = 1)    return result+1;for (int i = 0; i<len; i++) {if (i + nums[i] >= Pos) {result++;p os = I;break;}}}}

Then, referring to other people's answers, it was found that the general use of BFS (breadth/Width first search convenience) or greedy (greedy algorithm) to answer.

Then I chose the greedy algorithm.

The result is 2ms, but just time out of the set of test data, run time also shows 1ms, so for why just the code time-out and confused, see Leetcode on the discussion also did not find the corresponding solution.

1. Here is the maximum distance to record the current position and the number of steps (maxlen and result), plus the max distance (prepos) for each step.

2. To the next number, the maximum distance that the next number can reach is of course Newlen = I+nums[i], and then Newlen is compared to maxlen if the current number is farther away than previously calculated.

2.1 Then the current position I is compared with the maximum distance prepos of the current step result, if the prepos is more than result++, and the Prepos is set to the current maximum distance.

Set the maximum distance to Newlen after 2.2. At the same time can be judged, if MaxLen reached the len-1, then it means that can reach the end.

public class Solution {public    static int jump (int[] nums) {int len = nums.length;if (len < 2) return 0;int MaxLen = Nums[0];int Newlen = 0;int result = 1, Prepos = 0;if (maxlen >= len-1)            return result;for (int i =0;i<len;i++) {n Ewlen = i + nums[i];if (Newlen > MaxLen) {if (Prepos < i) {prepos = maxlen;result++;} MaxLen = newlen;if (maxlen >= len-1) return result;}} return result;}}

Leetcode Jump Game II----java

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.