Advertisement
ogv

Untitled

ogv
Aug 16th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. /*
  2. Runtime: 185 ms, faster than 24.87% of Java online submissions for Jump Game.
  3. Memory Usage: 40.9 MB, less than 52.14% of Java online submissions for Jump Game.
  4. */
  5. class Solution {
  6.     public boolean canJump(int[] nums) {
  7.         int len = nums.length;
  8.         if (len == 0) return false;
  9.         if (len == 1) return true;
  10.        
  11.         boolean[] visited = new boolean[len - 1];
  12.         visited[0] = true;
  13.        
  14.         for (int i = 0; i < len - 1; i++) {
  15.             if (!visited[i]) {
  16.                 continue;
  17.             }
  18.            
  19.             int maxReach = i + nums[i];
  20.             if (maxReach >= len - 1 || maxReach < 0) {
  21.                 return true;
  22.             }
  23.            
  24.             for (int k = i + 1; k <= maxReach; k++) {
  25.                 visited[k] = true;
  26.             }
  27.         }
  28.        
  29.         return false;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement