leetcode -- Jump Game II
2013-09-06 15:03
495 查看
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.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =
The minimum number of jumps to reach the last index is
[解题思路]
1.DP: let F(i) denote the minimum number of jumps, then we have F(i) = min(F(j) ) + 1 where j = 0, … i – 1 && A[j] + j >=i, this is O(N^2) approach and will get TLE by the OJ
2.Greedy
have bugs, the problem in the code: do not understand or realize when we need to do hops++!!!
That is to say: i did not find the way to solove this question
updated 20130910
inspired by the discussion in leetcode and http://tech-wonderland.net/blog/leetcode-jump-game-ii.html
the keypoint of solving the problem by greedy approach is that we should keep the current maxium reachable distance, the next maxium reachable distance and also the steps needed to do it.
when the index exceed the current maxium reachable distance, then we need to update it by the next maxium reachable distance and increase the steps by steps ++,
because when the index exceed the current maxium reachable distance, it means that we are stuck in current maxium reachable distance, we need to jump to increase the next maxium distance.
Each element in the array represents your maximum jump length 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
1step from index 0 to 1, then
3steps to the last index.)
[解题思路]
1.DP: let F(i) denote the minimum number of jumps, then we have F(i) = min(F(j) ) + 1 where j = 0, … i – 1 && A[j] + j >=i, this is O(N^2) approach and will get TLE by the OJ
public int jump(int[] A) { // Start typing your Java solution below // DO NOT write main() function int len = A.length; if(len <= 1){ return 0; } int[] f = new int[len]; for(int i = 0; i < len; i++){ f[i] = Integer.MAX_VALUE; } f[0] = 0; for(int i = 1; i < len; i++){ for(int j = 0; j < i; j++){ if(A[j] + j >= i){ f[i] = Math.min(f[i], f[j] + 1); } } } return f[len - 1]; }
2.Greedy
have bugs, the problem in the code: do not understand or realize when we need to do hops++!!!
That is to say: i did not find the way to solove this question
public int jump(int[] A) { // Start typing your Java solution below // DO NOT write main() function int len = A.length; if(len <= 1){ return 0; } int maxDis = A[0] + 0; int hops = 0; if(maxDis >= len - 1){ return hops + 1; } int i = maxDis; for(; i < len; i++){ if(A[i] + i > maxDis){ maxDis = A[i] + i; hops += 1; i = maxDis; } if(maxDis >= len - 1){ break; } } if(i == len - 1){ return hops; } else{ return hops + 1; } }
updated 20130910
inspired by the discussion in leetcode and http://tech-wonderland.net/blog/leetcode-jump-game-ii.html
the keypoint of solving the problem by greedy approach is that we should keep the current maxium reachable distance, the next maxium reachable distance and also the steps needed to do it.
when the index exceed the current maxium reachable distance, then we need to update it by the next maxium reachable distance and increase the steps by steps ++,
because when the index exceed the current maxium reachable distance, it means that we are stuck in current maxium reachable distance, we need to jump to increase the next maxium distance.
public int jump(int[] A) { // Start typing your Java solution below // DO NOT write main() function if(A == null){ return 0; } int len = A.length; if(len == 0 || len == 1){ return 0; } int cur = 0; int next = 0; int ret = 0; for(int i = 0; i < len; i++){ if(i > cur){ cur = next; ret++; } next = Math.max(next, i + A[i]); } return ret; }
相关文章推荐
- leetcode jump game ii
- LeetCode 第四十五题(Jump Game II)Java
- LeetCode--jump-game-ii
- leetcode解题报告:45. Jump Game II
- 【LeetCode-45】Jump Game II
- LeetCode题目 Jump Game II
- LEETCODE: Jump Game II
- LeetCode Jump Game && Jump GameII
- LeetCode Jump Game II
- LeetCode Jump Game II
- Leetcode NO.45 Jump Game II
- [LeetCode] Jump Game II
- LeetCode:Jump Game II
- LeetCode | Jump Game II
- Leetcode: Jump Game II
- [Leetcode] Jump Game II
- LeetCode--Jump Game II
- leetcode - Jump Game II
- leetcode: Jump Game II
- [LeetCode] Jump Game II 跳跃游戏之二