package leetcode;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

public class Problem55 {
    public boolean canJump(int[] nums) {
        Stack<Integer> stack = new Stack<>();
        Set<Integer> set = new HashSet<>();
        stack.add(0);
        while (!stack.isEmpty()) {
            int idx = stack.pop();
            if (idx == nums.length-1) {
                return true;
            }
            int val = nums[idx];
            for (int i = idx+1; i <= idx+val; i++) {
                if (i == nums.length-1) {
                    return true;
                }
                if (set.contains(i)) {
                    continue;
                }
                set.add(i);
                stack.push(i);
            }
        }
        return false;
    }
    
    public static void main(String[] args) {
        Problem55 prob = new Problem55();
        System.out.println(prob.canJump(new int[]{2,3,1,1,4})); // true
        System.out.println(prob.canJump(new int[]{3,2,1,0,4})); // false
        System.out.println(prob.canJump(new int[]{2,0})); // true
        System.out.println(prob.canJump(new int[]{0})); // true
        System.out.println(prob.canJump(new int[]{0,2,3})); // false
        System.out.println(prob.canJump(new int[]{1,0,1,0})); // false
    }
}

@@ -16,81 +16,8 @@
 e;%0A%0A
-import java.util.HashSet;%0Aimport java.util.Set;%0Aimport java.util.Stack;%0A%0A
 publ
@@ -90,205 +90,12 @@
     
-Stack%3CInteger%3E stack = new Stack%3C%3E();%0A        Set%3CInteger%3E set = new HashSet%3C%3E();%0A        stack.add(0);%0A        while (!stack.isEmpty()) %7B%0A            int idx = stack.pop();%0A            if (idx == 
+if (
 nums
@@ -93,34 +93,37 @@
  if (nums.length
--1
+ == 0
 ) %7B%0A            
@@ -114,36 +114,32 @@
 ) %7B%0A            
-    
 return true;%0A   
@@ -127,36 +127,32 @@
    return true;%0A
-    
         %7D%0A      
@@ -157,27 +157,21 @@
     
-    
 int 
-val
+x
  = nums%5B
 idx%5D
@@ -170,18 +170,12 @@
 ums%5B
-idx%5D;%0A    
+0%5D;%0A
     
@@ -195,164 +195,31 @@
 i = 
-idx+
 1; i %3C
-= idx+val; i++) %7B%0A                if (i == nums.length-1) %7B%0A                    return true;%0A                %7D%0A                if (set.contains(i)
+ nums.length; ++i
 ) %7B%0A
@@ -234,116 +234,21 @@
     
-  
-      continue;%0A                %7D%0A                set.add(i);%0A                stack.push(i);%0A            %7D
+if (x %3C= 0) %7B
 %0A   
@@ -244,34 +244,32 @@
 %3C= 0) %7B%0A        
-%7D%0A
         return f
@@ -282,516 +282,90 @@
     
-%7D%0A
     
-%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem55 prob = new Problem55();%0A        System.out.println(prob.canJump(new int%5B%5D%7B2,3,1,1,4%7D)); // true%0A        System.out.println(prob.canJump(new int%5B%5D%7B3,2,1,0,4%7D)); // false%0A        System.out.println(prob.canJump(new int%5B%5D%7B2,0%7D)); // true%0A        System.out.println(prob.canJump(new int%5B%5D%7B0%7D)); // true%0A        System.out.println(prob.canJump(new int%5B%5D%7B0,2,3%7D)); // false%0A        System.out.println(prob.canJump(new int%5B%5D%7B1,0,1,0%7D)); // false
+%7D%0A            x = Math.max(x - 1, nums%5Bi%5D);%0A        %7D%0A        return true;
 %0A   

Solve problem 55