【LeetCode】Jump Game (一维动态规划 + 线性扫描)
2013-10-30 22:54
417 查看
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 =
A =
java code : 这题初看用DP的思路很清晰: 设DP[i] := 从第 i 个位置开始能否到底最后一个位置,那么转移方程很好写
dp[A.length - 1] = true; 看代码吧,不过会超时,因为复杂度是O(n^2)
下面给出一种O(n)的算法:
我们用maxlength 维护一个从开始位置能到达的最远距离,然后判断在当前位置是否能够到底最后一个位置和当前位置是否可达,如果两个条件都满足,那么返回true,如果当前位置是0,并且最远距离不能超过当前位置,那么只能返回false 了,更新最远距离
java code : 416ms
顺便说一下我看讨论区有人贴出如此算法并且还过了:
这个算法是错误的,因为有一组反列: A = {5,4,0,0,0,0,0}, 显然应该返回 false.
leetcode 的数据还是有点弱。
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 =
[2,3,1,1,4], return
true.
A =
[3,2,1,0,4], return
false.
java code : 这题初看用DP的思路很清晰: 设DP[i] := 从第 i 个位置开始能否到底最后一个位置,那么转移方程很好写
dp[A.length - 1] = true; 看代码吧,不过会超时,因为复杂度是O(n^2)
public boolean canJump(int[] A) { if(A.length <= 1) return true; if(A[0] >= (A.length - 1)) return true; boolean[] dp = new boolean[A.length]; dp[A.length - 1] = true; for(int i = A.length - 2; i >= 0; i--) { if(i + A[i] >= A.length - 1) { dp[i] = true; continue; } for(int j = i + 1; j < A.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; }
下面给出一种O(n)的算法:
我们用maxlength 维护一个从开始位置能到达的最远距离,然后判断在当前位置是否能够到底最后一个位置和当前位置是否可达,如果两个条件都满足,那么返回true,如果当前位置是0,并且最远距离不能超过当前位置,那么只能返回false 了,更新最远距离
java code : 416ms
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(A.length <= 1) return true; if(A[0] >= (A.length - 1)) return true; int maxlength = A[0]; if(maxlength == 0) return false; for(int i = 1; i < A.length - 1; i++) { if(maxlength >= i && (i + A[i]) >= A.length - 1) return true; if(maxlength <= i && A[i] == 0) return false; if((i + A[i]) > maxlength) maxlength = i + A[i]; } return false; } }
顺便说一下我看讨论区有人贴出如此算法并且还过了:
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; } };
这个算法是错误的,因为有一组反列: A = {5,4,0,0,0,0,0}, 显然应该返回 false.
leetcode 的数据还是有点弱。
相关文章推荐
- 【LeetCode】Jump Game (一维动态规划 + 线性扫描)
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
- 【一维dp_线性扫描】Word Break 、Best time to Buy and Sell Stocks |||、max subarray、jump game |||
- LeetCode总结 -- 一维动态规划篇
- LeetCode题目:Unique Binary Search Trees,一维动态规划
- 算法刷题:LeetCode中常见的动态规划题目
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
- 线性动态规划基础
- LeetCode中的动态规划题目解答(3)
- 算法学习之动态规划(leetcode 174. Dungeon Game)
- leetcode 300. Longest Increasing Subsequence 最长递增子序列LISS + 十分经典的动态规划DP做法
- 动态规划第五讲——leetcode上的题目动态规划汇总(上)
- 线性动态规划 (共六题)
- LeetCode 198 House Robber(简单的动态规划)
- 动态规划之01背包问题及leetcode实例
- caioj1064:动态规划入门(一维一边推2:最长上升子序列)
- 动态规划之Leetcode 72. Edit Distance
- 动态规划----线性动态规划
- 算法:动态规划——线性模型之小朋友过桥