55. Jump Game
- Get link
- X
- Other Apps
Approach
calculate the maxIndex possible from every point if index is maximum from maxIndex than return false else return true at end
Complexity
- Time complexity: O(n)
- Space complexity: O(n)
Code
Java
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;
}
}- Get link
- X
- Other Apps
Comments
Post a Comment