[JavaScript] Backtracking + Dynamic Programming
javascript\nvar stoneGameIX = function (stones) {\n\n    // get the mod 3 of each stone and count each of their frequencies.\n    // We only care up to the numb
```javascript\nvar stoneGameIX = function (stones) {\n\n    // get the mod 3 of each stone and count each of their frequencies.\n    // We only care up to the number 3 since we care of if the sum of\n    // the numbers are able to be mod by 3\n    const freqCount = new Array(3).fill(0);\n    for (const stone of stones) {\n        freqCount[stone % 3]++;\n    }\n\n    // Since 2 0\'s cancel each other out we can remove them from the \n    // frequency counts to save on the number of backtrack calls\n    freqCount[0] = freqCount[0] % 2;\n    \n    let sum = 0;\n    const cache = {};\n    // backtrack and return true if the current player wins\n    const backtrack = (isAliceTurn) => {\n        // if there are no stones remaining return Bob winning\n        const numStonesRemaining = freqCount.reduce((sum, count) => sum + count);\n        if (numStonesRemaining === 0) return !isAliceTurn;\n\n        // check the cache to see if we have visited this result before\n        const cacheKey = String(freqCount)\n        if (cache[cacheKey] !== undefined) return cache[cacheKey];\n\n        for (let i = 0; i < 3; i++) {\n            // if there are no more of those number or if we lose\n            // skip the number\n            if (freqCount[i] === 0 || (sum + i) % 3 === 0) continue;\n            \n            freqCount[i]--;\n            sum += i;\n\n            // We need to save the result of the backtrack call\n            // so that we can put back the values\n            const canWeWin = !backtrack(!isAliceTurn);\n            \n            freqCount[i]++;\n            sum -= i;\n\n            // If we can win this round we return true\n            if (canWeWin) {\n                return cache[cacheKey] = true;\n            }\n        }\n        return cache[cacheKey] = false;\n    }\n    return backtrack(true);\n};\n```
['Dynamic Programming', 'Backtracking', 'JavaScript']