CPP Solution with dfs and memorize
\n\nclass Solution {\npublic:\n    vector<vector<vector<long int>>> save;\n    int dfs(int n, vector<int>& rollMax ,int last, int num){\n        long int cnt=0;
```\n\nclass Solution {\npublic:\n    vector<vector<vector<long int>>> save;\n    int dfs(int n, vector<int>& rollMax ,int last, int num){\n        long int cnt=0;\n        if(n==0){\n            return 1;\n        }\n        for(int i=0; i!=6 ;i++){\n                if(last==i){\n                    if(save[i][n-1][num+1]!=0)cnt+=save[i][n-1][num+1];\n                    else if(num<rollMax[i])cnt=cnt+dfs(n-1,rollMax,i,num+1);\n                }\n                else{\n                    if(save[i][n-1][1]!=0)cnt+=save[i][n-1][1];\n                    else cnt=cnt+dfs(n-1,rollMax,i,1);\n                }\n        }\n        \n        if(last!=-1)save[last][n][num]=cnt%1000000007;\n        return cnt%1000000007;\n    }\n    int dieSimulator(int n, vector<int>& rollMax) {\n        save.resize(6);\n\t    for (int i=0; i!=6; i++) {\n\t\t    save[i].resize(n+1);\n\t\t    for (int j=0; j!=n+1; j++)\n\t\t\t    save[i][j].resize(20);\n\t    }\n        for(int i=0; i!=6 ;i++){\n            for(int j=0; j!=n+1; j++){\n                for(int k=0; k!=20; k++)\n                    save[i][j][k]=0;\n            }\n        }\n        return dfs(n,rollMax,-1,0);\n    }\n};\n```
[]