Leetcode: Jump Game
2013-10-29 11:16
309 查看
http://oj.leetcode.com/problems/jump-game/
class Solution { public: // Don't code too fast // The bad dp algorithm below (commented out) has the complexity of O(N*N) /*bool search(int A[], int n, int current, vector<int> &dp){ if(dp[current]!=-1) return dp[current]==1; dp[current]=0; for(int last=min(n-1, current+A[current]);last>current;last--){ if(search(A,n,last,dp)){ dp[current]=1; break; } } return dp[current]==1; } bool canJump(int A[], int n) { vector<int> dp; // -1 unknown 0 unreachable 1 reachable for(int i=0;i<n-1;i++) dp.push_back(-1); dp.push_back(1); return search(A,n,0,dp); }*/ bool canJump(int A[], int n) { int lowestIndex=n-1; for(int i=n-2;i>=0;i--){ if((i+A[i])>=lowestIndex) lowestIndex=i; } return lowestIndex==0; } };
相关文章推荐
- [LeetCode] Jump Game 跳跃游戏
- LeetCode刷题【Array】 Jump Game
- leetcode-55 Jump Game 最远距离问题
- LeetCode | Jump Game
- 第六周:(LeetCode 45 55) Jump Game I II(c++)
- leetcode-jump game
- [LeetCode]Jump Game
- Jump Game--LeetCode
- LeetCode-55-Jump Game(C语言实现)
- LeetCode解题报告 55. Jump Game [medium]
- LeetCode 55 Jump Game(Python 实现及详解)
- [leetcode] 55. Jump Game 解题报告
- [Leetcode] - Jump Game
- leetcode--Jump Game
- LeetCode- Jump game
- leetcode 54: Jump Game
- Leetcode 55 Jump Game
- [LeetCode]55 Jump Game
- leetcode 55. Jump Game(C语言,跳数,递归思想,两种解法)20
- LeetCode(一) jump game