Jump Game I
Description
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.
Code
class Solution {
public:
/*
* @param A: A list of integers
* @return: A boolean
*/
bool canJump1(vector<int> &A) {
// write your code here
/*
vector<bool> dp(A.size(), false);
dp[0] = true;
*/
bool rst = false;
int maxReach = 0;
for(int loop = 0; loop < A.size() && loop <= maxReach; ++loop) {
if(0 == loop)
maxReach += A[loop];
else {
maxReach = max(loop + A[loop], maxReach);
}
if(maxReach >= A.size() - 1) {
rst = true;
break;
}
}
return rst;
}
//dp
bool canJump(vector<int> &A) {
/*
dp[i] = true if dp[j : j < i] && A[j] + j > i
*/
}
};