Created
October 8, 2018 23:21
-
-
Save shubhampateliitm/a035a5e592e150be9e542494fa0be4ca 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 1 ( true ). A = [3,2,1,0,4], return 0 ( false ). Return 0/1 for this problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int Solution::canJump(vector<int> &A) { | |
int sz = A.size(); | |
int lastIndex = sz-1; | |
int i = 0; | |
int reachable = A[i]; | |
while(i<=reachable){ | |
reachable = max(reachable,i+A[i]); | |
if(reachable >= lastIndex){ | |
return 1; | |
} | |
i++; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment