JavaScript O(n ** 2) DFA Solution
Intuition\nUse DFA.\n# Approach\nConsider each step as a transition in the DFA.\n# Complexity\n- Time complexity:\nO(n ** 2)\n- Space complexity:\nO(n ** 2)\n# 
# Intuition\nUse DFA.\n# Approach\nConsider each step as a transition in the DFA.\n# Complexity\n- Time complexity:\nO(n ** 2)\n- Space complexity:\nO(n ** 2)\n# Code\n```\n/**\n * @param {number[]} stones\n * @return {boolean}\n */\nvar stoneGameIX = function(stones) {\n    let a = 0, b = 0\n    for (const stone of stones) {\n        const rmd = stone % 3\n        if (rmd === 1)  a++\n        else if (rmd === 2) b++\n    }\n    const c = (stones.length - a - b) & 1\n    \n    \n    function createTable(dims) {\n        const size = dims.length\n        if (size === 1) {\n            const result = new Array(dims[0])\n            for (const i of result.keys())\n                result[i] = new Map()\n\n            return result\n        }\n            \n        \n        const dim = dims[0]\n        const table = new Array(dim)\n        const rest = dims.slice(1)\n        for (const i of table.keys())\n            table[i] = createTable(rest)\n\n        return table\n    }\n    const table = createTable([2, 2, 1 + c])\n\n\n    const A = 0, B = 1\n    const Alice = 0, Bob = 1\n    function dp(player, state, x, y, z) {\n        if (x === 0 && y === 0 && z === 0) {\n            return player === Bob\n        }\n\n        const map = table[player][state][z]\n        const key = `${x},${y}`\n        const existing = map.get(key)\n        if (existing !== undefined) return existing\n\n        let result = false\n        if (state === A) {\n            if (!result && x > 0 && !dp(1 - player, B, x - 1, y, z)) {\n                result = true\n            }\n            if (!result && z > 0 && !dp(1 - player, A, x, y, z - 1)) {\n                result = true\n            }\n        } else {\n            if (!result && y > 0 && !dp(1 - player, A, x, y - 1, z)) {\n                result = true\n            }\n            if (!result && z > 0 && !dp(1 - player, B, x, y, z - 1)) {\n                result = true\n            }\n        }\n\n        map.set(key, result)\n        return result\n    }\n\n\n    if (a > 0 && !dp(Bob, A, a - 1, b, c))    return true\n    if (b > 0 && !dp(Bob, B, a, b - 1, c))    return true\n\n\n    return false\n};\n```
['JavaScript']