LeetCode2029. 石子游戏 IX

相关信息:

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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值