[Java] Share my DP solution
It is a 2D-DP problem. \nDP[i][j] means how many choic for total i dices and the last number is j. \nFor each DP[i][j] we need to remove some invalid choice sin
It is a 2D-DP problem. \nDP[i][j] means how many choic for total i dices and the last number is j. \nFor each DP[i][j] we need to remove some invalid choice since there is a restriction for consecutive times.\n\nLet\'s take an example:\nAssuming restriction for 1 is 2 times and we meet the case **axxb** (a, b are indexes of uncertain values). We all know we can not take 1 at index-b when xx = \'11\', these are all invaild so we need to remove them.\n\nFirst of all, the total possible cases of dp[b][1] are sum of dp[b-1][1~6]\nfor index-b if we want to choose 1 so we need to consider the case that the two consecutive number before b is \'11\'. Also we need to be careful about value at the index-a. It shouldn\'t be \'1\'.  In short we need to remove all possible cases of **a11** **and a is not 1**.\nThe transition equation = **dp[b][1] = sum(dp[b-1][1~6]) - sum(dp[a][2~6(except 1)])**\n\nFollowing is my code:\nI save the sum of dp[i][1~6] at dp[i][7] for easier calculation. \n```\nclass Solution {\n    public int dieSimulator(int n, int[] rollMax) {\n        long divisor = (long)Math.pow(10, 9) + 7;\n        long[][] dp = new long[n][7];\n        for(int i = 0; i < 6; i++) {\n            dp[0][i] = 1;\n        }\n        dp[0][6] = 6;\n        for(int i = 1; i < n; i++) {\n            long sum = 0;\n            for(int j = 0; j < 6; j++) {\n                dp[i][j] = dp[i - 1][6];\n                if(i - rollMax[j] < 0) {\n                    sum = (sum + dp[i][j]) % divisor;\n                }\n                else {\n                    if(i - rollMax[j] - 1 >= 0) dp[i][j] = (dp[i][j] - (dp[i - rollMax[j] - 1][6] - dp[i - rollMax[j] - 1][j])) % divisor + divisor;\n                    else dp[i][j] = (dp[i][j] - 1) % divisor;\n                    sum = (sum + dp[i][j]) % divisor;\n                }\n\n            }\n            dp[i][6] = sum;\n        }\n        return (int)(dp[n - 1][6]);\n    }\n    \n}\n```
[]