Java DP
\nclass Solution {\n    public boolean stoneGameIX(int[] stones) {\n        int zero=0, one=0, two=0;\n        for (int s : stones) {\n            if (s%3 == 0)
```\nclass Solution {\n    public boolean stoneGameIX(int[] stones) {\n        int zero=0, one=0, two=0;\n        for (int s : stones) {\n            if (s%3 == 0) zero++;\n            if (s%3 == 1) one++;\n            if (s%3 == 2) two++;\n        }\n        \n        Map<String, Boolean> dp =  new HashMap<>();\n        return helper(0, zero, one, two, 0, dp);\n        \n    }\n    \n    private boolean helper(int sum, int zero, int one, int two, int turn, Map<String, Boolean> dp) {\n        // sum of removed stones is divisible by 3 for previous player, current player wins\n        if (turn != 0 && sum % 3 == 0) return true;\n\n        if (zero == 0 && one == 0 && two == 0) {\n            // no stones, bob wins\n            return turn % 2 == 1;\n        }\n        String key = sum +"|"+zero%2+"|"+one%2+"|"+two%2;\n        if (dp.containsKey(key)) {\n            return dp.get(key);\n        }\n        \n        boolean r1 = true,r2=true, r3=true;\n        if (zero > 0) {\n            r1 = helper(sum% 3, zero - 1, one, two, turn+1, dp);\n        }\n        if (one > 0) {\n            r2 = helper((sum+1) % 3, zero, one-1, two, turn+1, dp);\n        }\n        if (two > 0) {\n            r3 = helper((sum+2) % 3, zero, one, two-1, turn+1, dp);\n        }\n        \n        // current player wins if he can find out a way for the other player to lose in next turn\n        boolean res = (!r1 || !r2 || !r3);\n        dp.put(key, res);\n\n        return res;\n\n    }\n}\n```
[]