55. Jump Game
Approach
Complexity
- Time complexity: O(n)
- Space complexity: O(n)
calculate the maxIndex possible from every point if index is maximum from maxIndex than return false else return true at end
class Solution {
public boolean canJump(int[] nums) {
int maxIndex = 0;
for(int i = 0; i < nums.length; i++){
if(maxIndex < i){
return false;
}else{
maxIndex = Math.max(maxIndex,i + nums[i]);
}
}
return true;
}
}
Comments
Post a Comment