Skip to content

Commit 4f1c055

Browse files
committed
810
1 parent df5ed03 commit 4f1c055

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ Now I'm using a Chrome Extension I developed -- [LeetCoder](https://chrome.googl
649649
806 | Number of Lines To Write String | Easy | [Solution](leetcode/806.%20Number%20of%20Lines%20To%20Write%20String)
650650
807 | Max Increase to Keep City Skyline | Medium | [Solution](leetcode/807.%20Max%20Increase%20to%20Keep%20City%20Skyline)
651651
809 | Expressive Words | Medium | [Solution](leetcode/809.%20Expressive%20Words)
652+
810 | Chalkboard XOR Game | Hard | [Solution](leetcode/810.%20Chalkboard%20XOR%20Game)
652653
811 | Subdomain Visit Count | Easy | [Solution](leetcode/811.%20Subdomain%20Visit%20Count)
653654
812 | Largest Triangle Area | Easy | [Solution](leetcode/812.%20Largest%20Triangle%20Area)
654655
813 | Largest Sum of Averages | Medium | [Solution](leetcode/813.%20Largest%20Sum%20of%20Averages)
@@ -1281,6 +1282,10 @@ Now I'm using a Chrome Extension I developed -- [LeetCoder](https://chrome.googl
12811282
1797 | Design Authentication Manager | Medium | [Solution](leetcode/1797.%20Design%20Authentication%20Manager)
12821283
1798 | Maximum Number of Consecutive Values You Can Make | Medium | [Solution](leetcode/1798.%20Maximum%20Number%20of%20Consecutive%20Values%20You%20Can%20Make)
12831284
1799 | Maximize Score After N Operations | Hard | [Solution](leetcode/1799.%20Maximize%20Score%20After%20N%20Operations)
1285+
1800 | Maximum Ascending Subarray Sum | Easy | [Solution](leetcode/1800.%20Maximum%20Ascending%20Subarray%20Sum)
1286+
1801 | Number of Orders in the Backlog | Medium | [Solution](leetcode/1801.%20Number%20of%20Orders%20in%20the%20Backlog)
1287+
1802 | Maximum Value at a Given Index in a Bounded Array | Medium | [Solution](leetcode/1802.%20Maximum%20Value%20at%20a%20Given%20Index%20in%20a%20Bounded%20Array)
1288+
1803 | Count Pairs With XOR in a Range | Hard | [Solution](leetcode/1803.%20Count%20Pairs%20With%20XOR%20in%20a%20Range)
12841289

12851290
# License
12861291

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [810. Chalkboard XOR Game (Hard)](https://leetcode.com/problems/chalkboard-xor-game/)
2+
3+
<p>We are given non-negative integers nums[i] which are written on a chalkboard.&nbsp; Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first.&nbsp; If erasing a number causes&nbsp;the bitwise XOR of all the elements of the chalkboard to become&nbsp;0, then that player loses.&nbsp; (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.)</p>
4+
5+
<p>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.</p>
6+
7+
<p>Return True if and only if Alice wins the game, assuming both players play optimally.</p>
8+
9+
<pre><strong>Example:</strong>
10+
<strong>Input:</strong> nums = [1, 1, 2]
11+
<strong>Output:</strong> false
12+
<strong>Explanation:</strong>
13+
Alice has two choices: erase 1 or erase 2.
14+
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.
15+
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.
16+
17+
</pre>
18+
19+
<p><strong>Notes: </strong></p>
20+
21+
<ul>
22+
<li><code>1 &lt;= N &lt;= 1000</code>.&nbsp;</li>
23+
<li><code>0 &lt;= nums[i] &lt;= 2^16</code>.</li>
24+
</ul>
25+
26+
<p>&nbsp;</p>
27+
28+
29+
**Related Topics**:
30+
[Math](https://leetcode.com/tag/math/)
31+
32+
## Solution 1.
33+
34+
```cpp
35+
// OJ: https://leetcode.com/problems/chalkboard-xor-game/
36+
// Author: github.com/lzl124631x
37+
// Time: O(N)
38+
// Space: O(1)
39+
class Solution {
40+
public:
41+
bool xorGame(vector<int>& A) {
42+
int x = 0;
43+
for (int n : A) x ^= n;
44+
return A.size() % 2 == 0 || x == 0;
45+
}
46+
};
47+
```

0 commit comments

Comments
 (0)