Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean canJump(int[] nums) {
- if(nums.length == 1)
- return true;
- int target = nums.length-1;
- int maxJumpIndex = 0;
- for(int i = 0; i <= maxJumpIndex; ++i) { //check if we can surprass maxJumpIndex at any index before it
- if( (nums[i]+i) > maxJumpIndex) { //if we can go beyon
- if(nums[i] + i >= target) { //check if we can reach the target
- return true;
- }
- else {
- maxJumpIndex = nums[i]+i; //otherwise move to the furthest index we can go
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement