C# | Runtime beats 80.00%, Memory beats 66.67% [EXPLAINED]
Intuition\nImagine you\'re rolling a dice and you want to count all possible sequences of rolls. However, each face of the dice has a limit on how many times it
# Intuition\nImagine you\'re rolling a dice and you want to count all possible sequences of rolls. However, each face of the dice has a limit on how many times it can appear consecutively. For instance, if the limit is 2 for face 1, you can roll face 1 twice in a row but not three times. So, you need to count all valid sequences where no face appears more than its allowed consecutive times.\n\n# Approach\nDynamic Programming Table: We use a table dp[i][j][k] where:\n\ni is the number of rolls.\nj is the dice face (from 1 to 6).\nk is how many times face j appears consecutively.\nInitialization: For one roll, each face can appear once, so we start with dp[1][j][1] = 1 for all faces.\n\nFilling the Table:\n\nFor each roll count i, update the table based on previous roll counts:\nIf switching faces, add ways from the previous face counts.\nIf continuing the same face, just increase the consecutive count if it doesn\u2019t exceed the limit.\nResult: After processing all rolls, sum up all valid sequences for exactly n rolls.\n\n# Complexity\n- Time complexity:\n O(n * 6 * 6 * maxRollMax) which simplifies to O(n) in practice, since maxRollMax (maximum consecutive roll limit) is a small constant (15).\n\n- Space complexity:\n O(n * 6 * maxRollMax) which is manageable given the constraints.\n\n# Code\n```csharp []\npublic class Solution {\n    private const int MOD = 1000000007;\n    \n    public int DieSimulator(int n, int[] rollMax) {\n        // dp[i][j][k] => Number of ways to roll i times, ending with face j and k consecutive j\'s\n        int[,,] dp = new int[n + 1, 6, 16]; // We use 16 to cover the max rollMax[i] (which is 15)\n        \n        // Base case: For one roll, there\'s one way to get each face with 1 occurrence\n        for (int j = 0; j < 6; j++) {\n            dp[1, j, 1] = 1;\n        }\n        \n        // Iterate over the number of rolls\n        for (int i = 2; i <= n; i++) {\n            // Iterate over each possible dice face (j)\n            for (int j = 0; j < 6; j++) {\n                // Iterate over each previous dice face (prev)\n                for (int prev = 0; prev < 6; prev++) {\n                    if (prev != j) {\n                        // Transition from any previous dice face to a new face j\n                        for (int k = 1; k <= rollMax[prev]; k++) {\n                            dp[i, j, 1] = (dp[i, j, 1] + dp[i - 1, prev, k]) % MOD;\n                        }\n                    }\n                }\n                // Continue the current face j (consecutive rolls)\n                for (int k = 1; k < rollMax[j]; k++) {\n                    dp[i, j, k + 1] = (dp[i, j, k + 1] + dp[i - 1, j, k]) % MOD;\n                }\n            }\n        }\n        \n        // Sum all valid sequences of length n\n        int result = 0;\n        for (int j = 0; j < 6; j++) {\n            for (int k = 1; k <= rollMax[j]; k++) {\n                result = (result + dp[n, j, k]) % MOD;\n            }\n        }\n        \n        return result;\n    }\n}\n\n```
['C#']