package leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/dice-roll-simulation/
 */
public class Problem1223 {
    public int dieSimulator(int n, int[] rollMax) {
        return dieSimulator(n, rollMax, new HashMap<>());
    }

    private static int dieSimulator(int n, int[] rollMax, Map<String, Integer> memo) {
        if (n == 0) {
            return 1;
        }
        String key = n + "," + Arrays.toString(rollMax);
        if (memo.containsKey(key)) {
            return memo.get(key);
        }
        int count = 0;
        for (int i = 1; i <= 6; i++) {
            if (rollMax[i - 1] > 0) {
                rollMax[i - 1]--;
                count += dieSimulator(n - 1, rollMax, memo);
                rollMax[i - 1]++;
            }
        }
        memo.put(key, count);
        return count;
    }

    public static void main(String[] args) {
        Problem1223 prob = new Problem1223();
//        System.out.println(prob.dieSimulator(2, new int[]{1,1,2,2,2,3})); // 34
//        System.out.println(prob.dieSimulator(2, new int[]{1,1,1,1,1,1})); // 30
        System.out.println(prob.dieSimulator(3, new int[]{1,1,1,2,2,3})); // 181
//        System.out.println(prob.dieSimulator(1, new int[]{1,1,1,1,1,1})); // 6
    }
}

@@ -16,82 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -150,24 +150,71 @@
  rollMax) %7B%0A
+        int%5B%5D%5B%5D%5B%5D memo = new int%5B5000%5D%5B6%5D%5B16%5D;%0A
         retu
@@ -245,23 +245,19 @@
 ax, 
-new HashMap%3C%3E()
+-1, 0, memo
 );%0A 
@@ -324,181 +324,202 @@
 ax, 
-Map%3CString, Integer%3E memo) %7B%0A        if (n == 0) %7B%0A            return 1;%0A        %7D%0A        String key = n + %22,%22 + Arrays.toString(rollMax);%0A        if (memo.containsKey(key)
+int last, int currentLength,%0A                                    int%5B%5D%5B%5D%5B%5D memo) %7B%0A        if (n == 0) %7B%0A            return 1;%0A        %7D%0A        if (last %3E= 0 && memo%5Bn%5D%5Blast%5D%5BcurrentLength%5D %3E 0
 ) %7B%0A
@@ -545,17 +545,32 @@
 memo
-.get(key)
+%5Bn%5D%5Blast%5D%5BcurrentLength%5D
 ;%0A  
@@ -625,15 +625,14 @@
 i = 
-1
+0
 ; i %3C
-=
  6; 
@@ -658,26 +658,48 @@
 if (
-rollMax%5Bi - 1%5D %3E 0
+i == last && currentLength == rollMax%5Bi%5D
 ) %7B%0A
@@ -718,30 +718,32 @@
     
-rollMax%5Bi - 1%5D--;%0A
+continue;%0A        
     
+%7D%0A
     
@@ -760,10 +760,18 @@
 unt 
-+
 =
+ (count +
  die
@@ -800,548 +800,186 @@
 ax, 
-memo);%0A                rollMax%5Bi - 1%5D++;%0A            %7D%0A        %7D%0A        memo.put(key, count);%0A        return count;
+i, i == last ? currentLength + 1 : 1, memo)) %25 1_000_000_007;%0A        %7D
 %0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1223 prob = new Problem1223();%0A//        System.out.println(prob.dieSimulator(2, new int%5B%5D%7B1,1,2,2,2,3%7D)); // 34%0A//        System.out.println(prob.dieSimulator(2, new int%5B%5D%7B1,1,1,1,1,1%7D)); // 30%0A        System.out.println(prob.dieSimulator(3, new int%5B%5D%7B1,1,1,2,2,3%7D)); // 181%0A//        System.out.println(prob.dieSimulator(1, new int%5B%5D%7B1,1,1,1,1,1%7D)); // 6
+if (last %3E= 0) %7B%0A            memo%5Bn%5D%5Blast%5D%5BcurrentLength%5D = count;%0A        %7D%0A        return count;
 %0A   

Solve problem 1223