思路与算法:

image.png
image.png

细节:

image.png

代码:

  1. class Solution {
  2. public boolean stoneGameIX(int[] stones) {
  3. int cnt0 = 0, cnt1 = 0, cnt2 = 0;
  4. for (int val : stones) {
  5. int type = val % 3;
  6. if (type == 0) {
  7. ++cnt0;
  8. } else if (type == 1) {
  9. ++cnt1;
  10. } else {
  11. ++cnt2;
  12. }
  13. }
  14. if (cnt0 % 2 == 0) {
  15. return cnt1 >= 1 && cnt2 >= 1;
  16. }
  17. return cnt1 - cnt2 > 2 || cnt2 - cnt1 > 2;
  18. }
  19. }