JavaScript Depth First Search with Dynamic Programming
\n// In my solution, the function can only roll dice with the value of 0 to 5\n\nvar dieSimulator = function(n, rollMax) {\n  if (!n) return 0;\n  \n  // initia
```\n// In my solution, the function can only roll dice with the value of 0 to 5\n\nvar dieSimulator = function(n, rollMax) {\n  if (!n) return 0;\n  \n  // initialize cache with 7 possibilities\n  // the last cache will never be used but it makes the code easier to write\n  // because I will start my DFS with a roll of 6\n  const cache = [{},{},{},{},{},{},{}];\n  \n  // I limit for the number of possibilities [in prompt]\n  const limit = 1000000007;\n  \n  // I create the DFS helper function that will keep track of the total rolls,\n  // number of consecutive rolls, and the previous roll made through each\n  // recursive call\n  const dfs = (totalRolls, consecutiveRolls, prevRoll) => {\n  \n    // Since I need to check if all three cases are the same\n\t// I created a string so that we can immediately see if this\n\t// case has already been searched and we can immediately return the\n\t// result from our cache\n    const rollString = `${totalRolls}-${consecutiveRolls}`;\t\n    if (cache[prevRoll][rollString]) return cache[prevRoll][rollString];\n\t\n\t// if the total number of times has been rolled return 1;\n    if (totalRolls === n) return 1;\n\t\n\t// hold the value of the total number of rolls for the next roll\n\t// so that it doesn\'t get calculated through each iteration\n    const nextTotalRolls = totalRolls + 1;\n\t\n\t// create a counter that will sum all the possibilities from each next roll\n    let currCount = 0;\n\t\n\t// iterate from 0 to 5 to simulate rolling each value\n    for (let nextRoll = 0; nextRoll < 6; nextRoll++) {\n\t\n\t  // calculate the value of the next consecutive rolls\n      const nextConsecutiveRolls = nextRoll === prevRoll ? consecutiveRolls + 1 : 1;\n\t  \n\t  // if we have not exceeded the number of max consecutive rolls\n\t  // roll the dice with that value\n      if (nextConsecutiveRolls % (rollMax[nextRoll] + 1) !== 0) {\n\t  \n\t    // add the result of all possibilities to count at this stack;\n        currCount +=  dfs(nextTotalRolls, nextConsecutiveRolls, nextRoll);\n      }\n    }\n\t\n\t// store the result in the cache so it will be returned faster\n    cache[prevRoll][rollString] = currCount % limit;\n    return currCount % limit;\n  }\n  \n  // call the helper method with a value of 6 since it is available in the cache\n  // and 6 is not a possible dice roll in this solution\n  // the DFS is started with to handle the case if there is a rollMax value of 0\n  return dfs(0,0,6);\n};\n```
[]