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
.
public class Solution { public boolean canJump(int[] A) { // Start typing your Java solution below // DO NOT write main() function int i =0 ; int furthest = 0; while(i< A.length){ int temp = i + A[i]; if(temp > furthest) furthest = temp; if(furthest >= A.length -1){ return true; } if(furthest == i){ return false; } i++; } return true; } }
public class Solution { public boolean canJump(int[] A) { int end = 0; int max = 0; for(int start = 0; start <= max && start<A.length ; start++){ if(A[start] + start> max) max = A[start] + start; if(max>= A.length -1) return true; } return false; } }
mistake: without start<= max, if the first is 0 ,it will go next one