Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. class Solution {
  2.     public boolean canJump(int[] nums) {
  3.        
  4.         if(nums.length == 1)
  5.             return true;
  6.        
  7.         int target = nums.length-1;
  8.        
  9.         int maxJumpIndex = 0;
  10.        
  11.         for(int i  = 0; i <= maxJumpIndex; ++i) { //check if we can surprass maxJumpIndex at any index before it
  12.            
  13.             if( (nums[i]+i) > maxJumpIndex) { //if we can go beyon
  14.                
  15.                 if(nums[i] + i >= target) { //check if we can reach the target
  16.                     return true;
  17.                 }
  18.                 else {
  19.                     maxJumpIndex = nums[i]+i; //otherwise move to the furthest index we can go
  20.                 }
  21.                
  22.             }
  23.         }
  24.        
  25.         return false;
  26.        
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement