Skip to content

Instantly share code, notes, and snippets.

@pdu
Created February 17, 2013 12:06
Show Gist options
  • Select an option

  • Save pdu/4971233 to your computer and use it in GitHub Desktop.

Select an option

Save pdu/4971233 to your computer and use it in GitHub Desktop.
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 = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false. http://leetcode.com/onlinejudge#questio…
class Solution {
public:
bool canJump(int A[], int n) {
int dis = 0;
for (int i = 0; i < n; ++i) {
if (i <= dis)
dis = max(dis, i + A[i]);
else
break;
if (dis >= n - 1)
break;
}
return dis >= n - 1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment