package leetcode;

/**
 * https://leetcode.com/problems/stone-game-ii/
 */
public class Problem1140 {
    public int stoneGameII(int[] piles) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1140 prob = new Problem1140();
        System.out.println(prob.stoneGameII(new int[]{2, 7, 9, 4, 4})); // 10
        System.out.println(prob.stoneGameII(new int[]{2})); // 2
        System.out.println(prob.stoneGameII(new int[]{2, 3})); // 5
    }
}

@@ -149,32 +149,583 @@
     
-// TODO
+return stoneGame(piles, 1, true, 0);
 %0A    
+%7D%0A%0A
     
-return 0
+private static int stoneGame(int%5B%5D piles, int m, boolean alex, int index) %7B%0A        int answer = 0;%0A        for (int i = 1; i %3C= 2 * m; i++) %7B%0A            int sum = 0;%0A            for (int j = index; j %3C j + i && j %3C piles.length; j++) %7B%0A                sum += piles%5Bj%5D;%0A                int value = stoneGame(piles, Math.max(i, m), !alex, j + 1);%0A                if (alex) %7B%0A                    value += sum;%0A                    answer = Math.max(answer,  value);%0A                %7D%0A            %7D%0A        %7D%0A        return answer
 ;%0A  
@@ -820,16 +820,18 @@
 1140();%0A
+//
         
@@ -900,16 +900,89 @@
 ; // 10%0A
+        System.out.println(prob.stoneGameII(new int%5B%5D%7B2, 3, 4%7D)); // 5%0A//
         
@@ -1038,16 +1038,18 @@
 ); // 2%0A
+//
         

Update problem 1140