Concise Apporach with Explain - Bob wins too much
\nTime O(n)\nSpace O(1)\nRuntime: 116 ms, faster than 100.00% of C++ online submissions for Stone Game IX.\nMemory Usage: 127.8 MB, less than 11.11% of C++ onli
```\nTime O(n)\nSpace O(1)\nRuntime: 116 ms, faster than 100.00% of C++ online submissions for Stone Game IX.\nMemory Usage: 127.8 MB, less than 11.11% of C++ online submissions for Stone Game IX.\n```\n\n```\nThe algorithm:\nThe only problem is that bob wins for too many cases, especially when running out of all numbers\n```\n\n1. convert everything with (mod 3), and count;\n1.1 Now we have m[0], m[1], m[2];\n2. Alice starts the game with either 1 or 2\n2.1. Alice starts with 1 and the sequence will look like\n\n```\n1 * 1 * 2 * 1 * 2 ...\n```\nHere  * is a sequence of 0 with any length (possible empty). Bob looses only when he has no choice but pick 2 following  2 or 1 following 1, plus whether or not m[0] is odd, we have the following.\n```\nw1= m[1] && (!(m[0]&1) && m[2]>=m[1] || (m[0]&1) && m[1]-2>m[2]);\n```\n\n2.2. Similarly, Alice starts with 2 and the sequence will look like\n```\n2  * 2  * 1 * 2 * 1 ...\n```\nThis time, we consider the conditions of alice win.\n(a)  m[2]==1 and m[1]>0, bob have to pick 1 (and can pick 1 i.e. m[1]>0) in his turn without of choice when 2 is expected.\n(b)  m[2]>1,  bob have to pick 2 in his turn without of choice when 1 is expected.\n```\nw2= m[2]==1 && m[1]>0 && !(m[0]&1) || m[2]>1 && (m[2]-2<=m[1])^(m[0]&1);\n```\n\nThat\'s it.\n\n```\nclass Solution {\npublic:\n    bool stoneGameIX(vector<int>& stones) {\n        ios::sync_with_stdio(false);\n        cin.tie(nullptr);\n        int n=stones.size(), m[3]={0,0,0}, w1, w2;\n        for(int i=0; i<n; ++i) {\n            ++m[stones[i]%3];\n        }\n        //1: 1*1*2*1*2...\n        w1= m[1] && (!(m[0]&1) && m[2]>=m[1] || (m[0]&1) && m[1]-2>m[2]);\n        //2: 2*2*1*2*1...\n        w2= m[2]==1 && m[1]>0 && !(m[0]&1) || m[2]>1 && (m[2]-2<=m[1])^(m[0]&1);\n        return n>1 && (w1||w2);\n    }\n};\n```
[]