相关信息:
LeetCode链接:
https://leetcode-cn.com/problems/stone-game-ix/
代码:
//作者:LeetCode-Solution
public class L2029 {
public static void main(String[] args) {
int[] stones = {2,3,1,1,1,1};//{5, 1, 2, 4, 3};
Solution2029 solution2029 = new Solution2029();
boolean ans = solution2029.stoneGameIX(stones);
System.out.println(ans);
}
}
class Solution2029 {
public boolean stoneGameIX(int[] stones) {
/*cnti represents the number of I after element% 3 in the stones array, and the rounding range of I (0,1,2)
* {5, 1, 2, 4, 3} can be simplified to {2, 1, 2, 1, 0}
* */
int cnt0 = 0, cnt1 = 0, cnt2 = 0;
for (int val : stones) {
int type = val % 3;
if (type == 0) {
++cnt0;
} else if (type == 1) {
++cnt1;
} else {
++cnt2;
}
}
/*when cnt0 is even
* The element with% 3 as 0 in the stones array can be removed
* */
if (cnt0 % 2 == 0) {
return cnt1 >= 1 && cnt2 >= 1;
}
/*when cnt0 is odd alice and bob s best decision
* cnt2 - cnt1 = 0: stones数组{0,1,2} : A1 B0 A2 A 输
* cnt2 - cnt1 = 1: stones数组{0,1,2,2} : A1 B0 A2 B2 A 输
* cnt2 - cnt1 = 2: stones数组{0,1,2,2,2} : A1 B0 A2 B2 A2 A 输
* cnt2 - cnt1 = 2: stones数组{0,1,2,2,2,2} : A1 B0 A2 B2 A2 B2 B 输
* */
return cnt1 - cnt2 > 2 || cnt2 - cnt1 > 2;
}
}