Python3 DFS with memo and explanation
python\nclass Solution:\n    def dieSimulator(self, n: int, rollMax: List[int]) -> int:\n        from functools import lru_cache\n        ans = 0\n        self.
``` python\nclass Solution:\n    def dieSimulator(self, n: int, rollMax: List[int]) -> int:\n        from functools import lru_cache\n        ans = 0\n        self.memo = {}\n        M = 10**9 + 7\n        @lru_cache(None)\n        def dfs(k,index,n):\n            if n == 0:\n                return 1\n            if (k,index,n) in self.memo: \n\t\t\t#k means last dice num,index means this time\'s dice num, n means number of dices we left  \n                return self.memo[(k,index,n)]\n            \n            tmp = 0\n            for i in range(6):\n                if i != index:\n                    tmp += dfs(1, i, n-1) #different from last dice number\n                elif k < rollMax[i]: # same as last dice number without exceeding limit\n                    tmp += dfs(k + 1,i,n-1)\n                \n            self.memo[(k,index,n)] = tmp % M\n            return self.memo[(k,index,n)]\n        \n        return dfs(0,-1,n) % M\n```\n\nI appreciate it if u vote up! \u03A8(\uFFE3\u2200\uFFE3)\u03A8
[]