Code + Very Detailed Explanation
Preliminaries\nObservation 1: We only need to consider the stones modulo 3. Taking a stone that is 4, for example, is practically equivalent to taking a stone t
#### Preliminaries\n**Observation 1:** We only need to consider the stones modulo 3. Taking a stone that is 4, for example, is practically equivalent to taking a stone that is 1.\n\n**Observation 2:** The ordering of the stones don\'t matter. That is, stones {1, 2, 2} is effectively the same as stones {2, 1, 2}.\n\n**Consequence of Observation 1 and 2:** We only need to consider the number of stones that are 0, 1, and 2 mod 3. That will give enough information. The rest of the information is irrelevant.\n\n#### Part 1: Only Ones and Twos\nNow suppose that we simplify the problem a little bit and only consider the case in which no element is 0 modulo 3. That is, we have `x` stones that are 1 modulo 3 and `y` stones that are 2 modulo 3. Obviously, if either `x = 0` or `y = 0`, then Bob wins: nobody will pick something, which causes them to lose, and the game will end when all stones are taken (this by default means that Bob wins). \n\nWhat about the more interesting case when `x!=0` and `y!=0`? Then, suppose Alice takes a stone that is 1 modulo 3. Bob cannot take a stone that is 2 modulo 3 and hence must take a stone that is likewise 1 mod 3 (provided that it exists). After that, Alice takes stone 2, Bob takes stone 1, Alice takes stone 2, etc. The order of the stones taken looks like this: `112121212...`. On the other hand, if Alice picks a 2 first, then the order of the stones looks like this: `2212121212...`. It\'s not hard to show (or convice yourself) that in at least one of these cases, Alice will win. A simple argument is as follows: if there are more twoes than ones, then Alice should start by placing a one. If there are more ones than twoes, Alice should start by placing a 2.\n\n#### Part 2: Even Number of Zeroes\nWhat happens if there are an even number of zeroes? In such a case, it doesn\'t really matter actually! Suppose that had there been no zeroes, Alice would\'ve won. Then, with an even number of zeroes, Alice can still win (her strategy is to keep doing her winning strategy, but whenever Bob places a zero, she also places a zero). Likewise, if Bob would\'ve won with an even number of zeroes (i.e. there are no ones or twos), then Bob will still win because the game will terminate with no stones to pick from.\n\nSo, if there are an even number of zeroes but some ones and twos, Alice wins. Otherwise, if there are an even number of zeroes, but no ones or twos, then Bob wins.\n\n#### Part 3: Odd Number of Zeroes\nThe way I see the zeroes is as kind of passing the turn. You\'re not changing the result of the game, but you are making the other person move. So let\'s remove the zeroes from the game for a minute and see what happens. It looks like `112121212...` or `2212121212...`, as described before. In particular, let Alice play the majority number. Whenver Bob plays zero, she plays a zero. If Bob plays a nonzero number, then she plays a nonzero number. Eventually, this means that Alice can win but IF ONLY IF `abs(x - y) >= 3`. This may seem really arbitrary (you can convince yourself it\'s true with sample data, though). Even better, you can convince yourself with induction and dynamic programming.\n\n```\nclass Solution {\npublic:\n    bool stoneGameIX(vector<int>& stones) {\n        int count[3];\n        count[0] = count[1] = count[2] = 0;\n        for(int i = 0; i < stones.size(); i++) {\n            stones[i] %= 3;\n            count[stones[i]]++;\n        }\n        if (count[0] % 2 == 0) {\n            return (count[1] != 0 && count[2] != 0);\n        } else {\n            return (abs(count[2] - count[1]) >= 3);\n        }\n    }\n};\n```
[]