Weekly Contest 78-------->810. Chalkboard XOR Game
We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses. (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.)
Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins.
Return True if and only if Alice wins the game, assuming both players play optimally.
Example:
Input: nums = [1, 1, 2]
Output: false
Explanation:
Alice has two choices: erase 1 or erase 2.
If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose.
If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.
Notes:
1 <= N <= 1000.0 <= nums[i] <= 2^16.
Approach #1: C++.
class Solution {
public:
bool xorGame(vector<int>& nums) {
int num = 0;
for (int i : nums)
num ^= i;
return num == 0 || nums.size() % 2 == 0;
}
};
Approach #2: Java.
class Solution {
public boolean xorGame(int[] nums) {
int xo = 0;
for (int i : nums)
xo ^= i;
return xo == 0 || nums.length % 2 == 0;
}
}
Approach #3: Python.
class Solution(object):
def xorGame(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
xo = 0
for i in nums:
xo ^= i
return xo == 0 or len(nums) % 2 == 0
Analysis:
Math:
Corner Case: If the XOR of all nums is 0, then A wins.
Now we discuss the more general case where the input doesn't from the corner case.
Proposition: Suppose the current chalkboard is S and the len(S) = N, now it's player P's turn. P can
always make a move if XOR(S) != 0 and N is even.
Proof:
- Let X = XOR(S), when X != 0, at least one bit of X must be 1. Let bit 'b' of X be the bit ie., X[b] = 1.
- Then we can divide the numbers in S into two groups: U and V, where numbers in U have 0 at bit b, and numbers in V have 1 at bit b.
- Initially, len(U) could be even or odd, But len(V) must be odd, otherwise we wouldn't have X[b] = 1, So len(U) must be odd too because of the following:
- len(V) + len(U) = N
- len(V) is odd
- N is even
- The fact len(U) is odd implies that there must be at least one number (say Y) in S which has Y[b] = 0.
- If player P removes the number Y from S, the result chalkboard S' will have X' = XOR(S') = X xor Y, where X'[b] = 1. So S' != 0.
The explanation come from https://leetcode.com/problems/chalkboard-xor-game/discuss/165396/Detailed-math-explanation-Easy-to-understand
Weekly Contest 78-------->810. Chalkboard XOR Game的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- [Swift]LeetCode810. 黑板异或游戏 | Chalkboard XOR Game
We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob take tu ...
- [LeetCode] Chalkboard XOR Game 黑板亦或游戏
We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob take tu ...
- LintCode——Chalkboard XOR Game(黑板游戏)
黑板游戏: We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob ta ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
随机推荐
- PHP 5.6编译安装
yum install openssl openssl-devel libxml2-devel libxml2 bzip2 bzip2-devel curl-devel php-mcrypt libm ...
- switch中的case不加break执行情况
输出结果:230 分析,switch先匹配一个case满足$a,然后执行case里面的语句,直到遇到break,否则一直往下执行 <?php $a = ; switch($a){ : echo ...
- 模式识别之不变矩---SIFT和SURF的比较
- 面试题三:设计包括 min 函数的栈。
3.设计包括 min 函数的栈. 定义栈的数据结构,要求加入一个 min 函数.可以得到栈的最小元素. 要求函数 min.push 以及 pop 的时间复杂度都是 O(1). 思路分析: a.要想一个 ...
- SpringInAction4笔记——复习
由于目前只做后端的业务代码的开发,所以根据自己掌握的熟悉程度,只需要复习几个模块即可 重点看的是核心容器(IOC),redis,缓存,消息(主要是rabbitmq),事务,springboot,单元测 ...
- 微信分享配置(js-sdk)
现在的微信分享给朋友-分享到朋友圈 链接带有自定义的title.描述.图片,需要配置js-sdk(地址:mp.weixin.qq.com)微信文档 需要后台配置config的参数,返回给前台 1)de ...
- mapper代理(十一)
原始 dao开发问题 1.dao接口实现类方法中存在大量模板方法,设想能否将这些代码提取出来,大大减轻程序员的工作量. 2.调用sqlsession方法时将statement的id硬编码了 3.调用s ...
- [Java SE] 字符串连接
Java 支持多种字符串连接方式,总结如下: package cn.spads.tool.string; import java.text.MessageFormat; /** * <b> ...
- 阻止SSIS import excel时的默认行为
为什么SSIS总是错误地获取Excel数据类型,以及如何解决它! 由Concentra发布 2013年5月15日 分享此页面 分享 发现Concentra的分析解决方案 Concentra的分析和 ...
- Mac终端操作SVN指令
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domain ...