package leetcode;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * https://leetcode.com/problems/can-i-win/
 */
public class Problem464 {
    public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
        // TODO
        Set<Integer> nums = new TreeSet<>();
        for (int i = 1; i <= maxChoosableInteger; i++) {
            nums.add(i);
        }
        return canIWin(nums, desiredTotal, 0, true);
    }

    private static boolean canIWin(Set<Integer> nums, int desired, int accu, boolean player1) {
        if (accu >= desired) {
            if (!player1) {
                return true;
            }
            return false;
        }
        boolean win = (player1) ? false : true;
        for (int num : nums) {
            Set<Integer> newNums = new HashSet<>(nums);
            newNums.remove(num);
            if (player1) {
                win |= canIWin(newNums, desired, accu + num, !player1);
            } else {
                win &= canIWin(newNums, desired, accu + num, !player1);
            }
        }
        return win;
    }

    public static void main(String[] args) {
        Problem464 prob = new Problem464();
        System.out.println(prob.canIWin(10, 11)); // false
        System.out.println(prob.canIWin(4, 6)); // true
        System.out.println(prob.canIWin(10, 100)); // false
        System.out.println(prob.canIWin(10, 55)); // false
        System.out.println(prob.canIWin(6, 20)); // false
        System.out.println(prob.canIWin(6, 15)); // true
        System.out.println(prob.canIWin(6, 10)); // true
        System.out.println(prob.canIWin(5, 12)); // true
        System.out.println(prob.canIWin(5, 15)); // true
//        System.out.println(prob.canIWin(20, 200)); // false
    }
}

@@ -33,19 +33,67 @@
 til.Hash
-Set
+Map;%0Aimport java.util.HashSet;%0Aimport java.util.Map
 ;%0Aimport
@@ -488,16 +488,33 @@
  0, true
+, new HashMap%3C%3E()
 );%0A    %7D
@@ -603,24 +603,86 @@
 lean player1
+,%0A                                   Map%3CString, Boolean%3E memo
 ) %7B%0A        
@@ -803,32 +803,156 @@
 alse;%0A        %7D%0A
+        String key = accu + %22%7C%22 + player1;%0A        if (memo.containsKey(key)) %7B%0A            return memo.get(key);%0A        %7D%0A
         boolean 
@@ -1191,32 +1191,38 @@
  + num, !player1
+, memo
 );%0A            %7D
@@ -1298,16 +1298,22 @@
 !player1
+, memo
 );%0A     
@@ -1323,32 +1323,60 @@
     %7D%0A        %7D%0A
+        memo.put(key, win);%0A
         return w
@@ -1471,24 +1471,26 @@
 oblem464();%0A
+//
         Syst
@@ -1528,32 +1528,34 @@
  11)); // false%0A
+//
         System.o
@@ -1586,32 +1586,34 @@
 4, 6)); // true%0A
+//
         System.o
@@ -1648,32 +1648,34 @@
 100)); // false%0A
+//
         System.o
@@ -1709,32 +1709,34 @@
  55)); // false%0A
+//
         System.o
@@ -1773,24 +1773,26 @@
 ); // false%0A
+//
         Syst
@@ -1828,32 +1828,34 @@
 , 15)); // true%0A
+//
         System.o
@@ -1944,32 +1944,34 @@
 , 12)); // true%0A
+//
         System.o

Update problem 464