Simple Python Optimal Strategy Explained
Denote n0 as the number of stones that have % 3 == 0\nDenote n1 as the number of stones that have % 3 == 1\nDenote n2 as the number of stones that have % 3 == 2
Denote n0 as the number of stones that have % 3 == 0\nDenote n1 as the number of stones that have % 3 == 1\nDenote n2 as the number of stones that have % 3 == 2\nConsider the  simple case n0 == 0,\nAlice could win by taking the from the smaller number of n1 and n2, Bob would be forced to make a losing move. If n1 == n2, Alice also wins because the last choice from Bob would make sum % 3 == 0\n\nFor n0 % 2 == 0, the zeros act as "buffers", prolonging the game. Knowing from the case n0 == 0, Bob will lose if he doesn\'t(cannot) pick n0, so he will pick n0, but alice can counter that by also picking n0. Since n0 % 2 == 0, Alice still wins, so one may solve this case as if n0 == 0.\n\nFor n0 % 2 == 1: Alice may start from picking the larger pile of (n1,n2). by doing so, she forces Bob to make the losing move. The order of taking stones from n0 does not matter at all, the winning/losing scenario could be imagined by taking n1 or n2 first, and when one pile of (n1,n2) is depleted, and the other one is the losing move,  start taking stones from n0 until the game is resolved.\n\nNow we only need to deal with the cases where the stones are depleted, in which case Bob wins instead. For n0 % 2 == 0, if the smaller pile of (n1,n2) = 0, then Alice would be forced to choose from the larger pile, she would lose instead. For n0 % 2 == 1 if the difference between n1 and n2 is not greater than two, the stones would be depleted first, so she also loses. (Ex : n0 = 3, n1 = 3, n2 = 1, A1 -> B1 -> A2 -> B1 -> A0 -> B0 -> A0,  If there is at least one more n1, B1 would be forced and Alice wins, else Alice loses)\n\n```\nclass Solution:\n    def stoneGameIX(self, stones: List[int]) -> bool:\n        from collections import Counter\n        mod = Counter([x%3 for x in stones])\n        if mod[0] % 2 == 0:\n            if min(mod[1],mod[2]) == 0:\n                return False\n            else:\n                return True\n        else:\n            if abs(mod[1] - mod[2]) <= 2:\n                return False\n            return True\n```\n
[]