Jump Game (one-dimensional dynamic planning + linear scanning)

Source: Internet
Author: User

Given an array of non-negative integers, you are initially positioned at the first index of the array. each element in the array represents your maximum jump length at that position. determine if you are able to reach the last index. for example: A = [, 4], return true. A = [3, 2, 1, 0, 4], return false. java code: the idea of using DP is clear at the beginning: Set DP [I]: = whether the last position can be reached from position I, so the transfer equation is very easy to write dp [. length-1] = true; read Code, but it will time out, because the complexity is O (n ^ 2) [java] public boolean canJump (int [] A) {if (. length <= 1) return true; if (A [0]> = (. length-1) return true; boolean [] dp = new boolean [. length]; dp [. length-1] = true; for (int I =. length-2; I> = 0; I --) {if (I + A [I]> =. length-1) {dp [I] = true; continue;} for (int j = I + 1; j <. length-1; j ++) {if (I + A [I]> = j) {dp [I] | = dp [j]; if (dp [I] = true) break; }}} Boolean res = false; if (dp [0] = true) res = true; dp = null; return res;} an O (n) algorithm: We use maxlength to maintain the longest distance that can be reached from the starting position, and then determine whether the last and current positions can be reached at the current position. If both conditions are met, returns true. If the current position is 0 and the maximum distance cannot exceed the current position, only false is returned. Update the remote java code: 416 ms [java] public class Solution {public boolean canJump (int [] A) {// IMPORTANT: Please reset any member data you declared, as // the same Solution instance will Be reused for each test case. if (. length <= 1) return true; if (A [0]> = (. length-1) return true; int maxlength = A [0]; if (maxlength = 0) return false; for (int I = 1; I <. length-1; I ++) {if (maxlength> = I & (I + A [I])> =. length-1) return true; if (maxlength <= I & A [I] = 0) return false; if (I + A [I])> maxlength) maxlength = I + A [I];} return false;} By the way, I see someone posted such an algorithm in the discussion board and passed it back: [c Pp] class Solution {public: bool canJump (int A [], int n) {// IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int I = 0; while (I <n-1) {if (A [I] = 0) return false; I + = A [I];} return I> = n-1 ;}}; this algorithm is incorrect because there is A group of inverse columns: A = {5, 4, 0, 0, 0, 0}. Obviously, false should be returned. the leetcode data is still a little weak.

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.