Skip to content

Commit 7abb958

Browse files
committed
feat: add solutions to lc problem: No.0810
No.0810.Chalkboard XOR Game
1 parent 62e682f commit 7abb958

File tree

23 files changed

+539
-35
lines changed

23 files changed

+539
-35
lines changed

solution/0400-0499/0420.Strong Password Checker/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<ul>
1010
<li>It has at least <code>6</code> characters and at most <code>20</code> characters.</li>
1111
<li>It contains at least <strong>one lowercase</strong> letter, at least <strong>one uppercase</strong> letter, and at least <strong>one digit</strong>.</li>
12-
<li>It does&nbsp;not contain three repeating characters in a row (i.e.,&nbsp;<code>&quot;...aaa...&quot;</code> is weak, but <code>&quot;...aa...a...&quot;</code> is strong, assuming other conditions are met).</li>
12+
<li>It does not contain three repeating characters in a row (i.e., <code>&quot;B<u><strong>aaa</strong></u>bb0&quot;</code> is weak, but <code>&quot;B<strong><u>aa</u></strong>b<u><strong>a</strong></u>0&quot;</code> is strong).</li>
1313
</ul>
1414

1515
<p>Given a string <code>password</code>, return <em>the minimum number of steps required to make <code>password</code> strong. if <code>password</code> is already strong, return <code>0</code>.</em></p>
@@ -19,7 +19,7 @@
1919
<ul>
2020
<li>Insert one character to <code>password</code>,</li>
2121
<li>Delete one character from <code>password</code>, or</li>
22-
<li>Replace&nbsp;one character of <code>password</code> with another character.</li>
22+
<li>Replace one character of <code>password</code> with another character.</li>
2323
</ul>
2424

2525
<p>&nbsp;</p>

solution/0500-0599/0554.Brick Wall/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<strong>输出:</strong>3
2929
</pre>
3030

31+
32+
3133
<p><strong>提示:</strong></p>
3234

3335
<ul>

solution/0800-0899/0810.Chalkboard XOR Game/README.md

+82-1
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,103 @@ Alice 有两个选择: 擦掉数字 1 或 2。
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:位运算**
58+
59+
根据游戏规则,轮到某个玩家时,如果当前黑板上所有数字按位异或运算结果为 $0$,这个玩家获胜。由于 Alice 先手,因此当 `nums` 中所有数字的异或结果为 $0$ 时,Alice 可以获胜。
60+
61+
`nums` 中所有数字的异或结果不为 $0$ 时,我们不妨考虑从数组 `nums` 的长度奇偶性来分析 Alice 的获胜情况。
62+
63+
`nums` 的长度为偶数时,如果 Alice 必败,那么只有一种情况,就是 Alice 无论擦掉哪个数字,剩余所有数字的异或结果都等于 $0$。我们来分析一下是否存在这种情况。
64+
65+
假设数组 `nums` 长度为 $n$,并且 $n$ 是偶数,记所有数字异或结尾为 $S$,则有:
66+
67+
$$
68+
S = nums[0] \oplus nums[1] \oplus \cdots \oplus nums[n-1] \neq 0
69+
$$
70+
71+
我们记 $S_i$ 为数组 `nums` 擦掉第 $i$ 个数字后的异或结果,那么有:
72+
73+
$$
74+
S_i \oplus nums[i] = S
75+
$$
76+
77+
等式两边同时异或 $nums[i]$,得到:
78+
79+
$$
80+
S_i = S \oplus nums[i]
81+
$$
82+
83+
如果无论 Alice 擦掉哪个数字,剩余所有数字的异或结果都等于 $0$,那么对所有 $i$,都有 $S_i = 0$,即:
84+
85+
$$
86+
S_0 \oplus S_1 \oplus \cdots \oplus S_{n-1} = 0
87+
$$
88+
89+
我们将 $S_i = S \oplus nums[i]$ 代入上式,得到:
90+
91+
$$
92+
S \oplus nums[0] \oplus S \oplus nums[1] \oplus \cdots \oplus S \oplus nums[n-1] = 0
93+
$$
94+
95+
上式共有 $n$(偶数)个 $S$,而 $nums[0] \oplus nums[1] \oplus \cdots \oplus nums[n-1]$ 也等于 $S$,因此上式等价于 $0 \oplus S = 0$。这与 $S \neq 0$ 矛盾,因此不存在这种情况。因此当 `nums` 的长度为偶数时,Alice 必胜。
96+
97+
如果长度为奇数,那么 Alice 擦掉一个数字后,剩余数字个数为偶数,也就是将偶数长度的情况留给 Bob,那么 Bob 必胜,也即 Alice 必败。
98+
99+
综上,当 `nums` 的长度为偶数,或者 `nums` 中所有数字的异或结果为 $0$ 时,Alice 可以获胜。
100+
101+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
102+
57103
<!-- tabs:start -->
58104

59105
### **Python3**
60106

61107
<!-- 这里可写当前语言的特殊实现逻辑 -->
62108

63109
```python
64-
110+
class Solution:
111+
def xorGame(self, nums: List[int]) -> bool:
112+
return len(nums) % 2 == 0 or reduce(xor, nums) == 0
65113
```
66114

67115
### **Java**
68116

69117
<!-- 这里可写当前语言的特殊实现逻辑 -->
70118

71119
```java
120+
class Solution {
121+
public boolean xorGame(int[] nums) {
122+
return nums.length % 2 == 0 || Arrays.stream(nums).reduce(0, (a, b) -> a ^ b) == 0;
123+
}
124+
}
125+
```
126+
127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
bool xorGame(vector<int>& nums) {
133+
if (nums.size() % 2 == 0) return true;
134+
int x = 0;
135+
for (int& v : nums) x ^= v;
136+
return x == 0;
137+
}
138+
};
139+
```
72140
141+
### **Go**
142+
143+
```go
144+
func xorGame(nums []int) bool {
145+
if len(nums)%2 == 0 {
146+
return true
147+
}
148+
x := 0
149+
for _, v := range nums {
150+
x ^= v
151+
}
152+
return x == 0
153+
}
73154
```
74155

75156
### **...**

solution/0800-0899/0810.Chalkboard XOR Game/README_EN.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,48 @@ If Alice erases 2 first, now nums become [1, 1]. The bitwise XOR of all the elem
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def xorGame(self, nums: List[int]) -> bool:
58+
return len(nums) % 2 == 0 or reduce(xor, nums) == 0
5759
```
5860

5961
### **Java**
6062

6163
```java
64+
class Solution {
65+
public boolean xorGame(int[] nums) {
66+
return nums.length % 2 == 0 || Arrays.stream(nums).reduce(0, (a, b) -> a ^ b) == 0;
67+
}
68+
}
69+
```
70+
71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
bool xorGame(vector<int>& nums) {
77+
if (nums.size() % 2 == 0) return true;
78+
int x = 0;
79+
for (int& v : nums) x ^= v;
80+
return x == 0;
81+
}
82+
};
83+
```
6284
85+
### **Go**
86+
87+
```go
88+
func xorGame(nums []int) bool {
89+
if len(nums)%2 == 0 {
90+
return true
91+
}
92+
x := 0
93+
for _, v := range nums {
94+
x ^= v
95+
}
96+
return x == 0
97+
}
6398
```
6499

65100
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
bool xorGame(vector<int>& nums) {
4+
if (nums.size() % 2 == 0) return true;
5+
int x = 0;
6+
for (int& v : nums) x ^= v;
7+
return x == 0;
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func xorGame(nums []int) bool {
2+
if len(nums)%2 == 0 {
3+
return true
4+
}
5+
x := 0
6+
for _, v := range nums {
7+
x ^= v
8+
}
9+
return x == 0
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public boolean xorGame(int[] nums) {
3+
return nums.length % 2 == 0 || Arrays.stream(nums).reduce(0, (a, b) -> a ^ b) == 0;
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def xorGame(self, nums: List[int]) -> bool:
3+
return len(nums) % 2 == 0 or reduce(xor, nums) == 0

solution/0900-0999/0932.Beautiful Array/README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,37 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>对于某些固定的&nbsp;<code>N</code>,如果数组&nbsp;<code>A</code>&nbsp;是整数&nbsp;<code>1, 2, ..., N</code>&nbsp;组成的排列,使得:</p>
9+
<p>如果长度为 <code>n</code> 的数组 <code>nums</code> 满足下述条件,则认为该数组是一个 <strong>漂亮数组</strong> :</p>
1010

11-
<p>对于每个&nbsp;<code>i &lt; j</code>,都<strong>不存在</strong>&nbsp;<code>k</code> 满足&nbsp;<code>i &lt; k &lt; j</code>&nbsp;使得&nbsp;<code>A[k] * 2 = A[i] + A[j]</code>。</p>
12-
13-
<p>那么数组 <code>A</code>&nbsp;是漂亮数组。</p>
14-
15-
<p>&nbsp;</p>
11+
<ul>
12+
<li><code>nums</code> 是由范围 <code>[1, n]</code> 的整数组成的一个排列。</li>
13+
<li>对于每个 <code>0 &lt;= i &lt; j &lt; n</code> ,均不存在下标 <code>k</code>(<code>i &lt; k &lt; j</code>)使得 <code>2 * nums[k] == nums[i] + nums[j]</code> 。</li>
14+
</ul>
1615

17-
<p>给定&nbsp;<code>N</code>,返回<strong>任意</strong>漂亮数组&nbsp;<code>A</code>(保证存在一个)。</p>
16+
<p>给你整数 <code>n</code> ,返回长度为 <code>n</code> 的任一 <strong>漂亮数组</strong> 。本题保证对于给定的 <code>n</code> 至少存在一个有效答案。</p>
1817

1918
<p>&nbsp;</p>
2019

21-
<p><strong>示例 1:</strong></p>
20+
<p><strong class="example">示例 1 :</strong></p>
2221

23-
<pre><strong>输入:</strong>4
22+
<pre>
23+
<strong>输入:</strong>n = 4
2424
<strong>输出:</strong>[2,1,4,3]
2525
</pre>
2626

27-
<p><strong>示例 2:</strong></p>
27+
<p><strong class="example">示例 2 :</strong></p>
2828

29-
<pre><strong>输入:</strong>5
30-
<strong>输出:</strong>[3,1,2,5,4]</pre>
29+
<pre>
30+
<strong>输入:</strong>n = 5
31+
<strong>输出:</strong>[3,1,2,5,4]
32+
</pre>
3133

3234
<p>&nbsp;</p>
3335

3436
<p><strong>提示:</strong></p>
3537

3638
<ul>
37-
<li><code>1 &lt;= N &lt;= 1000</code></li>
39+
<li><code>1 &lt;= n &lt;= 1000</code></li>
3840
</ul>
3941

4042
<p>&nbsp;</p>

solution/1100-1199/1166.Design File System/README_EN.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ fileSystem.get(&quot;/c&quot;); // return -1 because this path doesn&#39;t exist
5353
<p><strong>Constraints:</strong></p>
5454

5555
<ul>
56-
<li>The number of&nbsp;calls to the two functions&nbsp;is less than or equal to <code>10<sup>4</sup></code> in total.</li>
5756
<li><code>2 &lt;= path.length &lt;= 100</code></li>
5857
<li><code>1 &lt;= value &lt;= 10<sup>9</sup></code></li>
58+
<li>Each <code>path</code> is <strong>valid</strong> and consists of lowercase English letters and <code>&#39;/&#39;</code>.</li>
59+
<li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>createPath</code> and <code>get</code>.</li>
5960
</ul>
6061

6162
## Solutions

solution/1500-1599/1527.Patients With a Condition/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This table contains information of the patients in the hospital.
2121

2222
<p>&nbsp;</p>
2323

24-
<p>Write an SQL query to report the patient_id, patient_name all conditions of patients who have Type I Diabetes. Type I Diabetes always starts with <code>DIAB1</code> prefix</p>
24+
<p>Write an SQL query to report the patient_id, patient_name and conditions of the patients who have Type I Diabetes. Type I Diabetes always starts with <code>DIAB1</code> prefix.</p>
2525

2626
<p>Return the result table in <strong>any order</strong>.</p>
2727

solution/2400-2499/2474.Customers With Strictly Increasing Purchases/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
| price | int |
1919
+--------------+------+
2020
order_id is the primary key for this table.
21-
Each row contains the id of an order, the id of
21+
Each row contains the id of an order, the id of customer that ordered it, the date of the order, and its price.
2222
</pre>
2323

2424
<p>&nbsp;</p>

solution/2400-2499/2474.Customers With Strictly Increasing Purchases/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
| price | int |
1717
+--------------+------+
1818
order_id is the primary key for this table.
19-
Each row contains the id of an order, the id of
19+
Each row contains the id of an order, the id of customer that ordered it, the date of the order, and its price.
2020
</pre>
2121

2222
<p>&nbsp;</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2479. Maximum XOR of Two Non-Overlapping Subtrees](https://leetcode.cn/problems/maximum-xor-of-two-non-overlapping-subtrees)
2+
3+
[English Version](/solution/2400-2499/2479.Maximum%20XOR%20of%20Two%20Non-Overlapping%20Subtrees/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>There is an undirected tree with n nodes labeled from <code>0</code> to <code>n - 1</code>. You are given the integer n and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
10+
11+
<p>Each node has an associated <strong>value</strong>. You are given an array <code>values</code> of length <code>n</code>, where <code>values[i]</code> is the <strong>value</strong> of the <code>i<sup>th</sup></code> node.</p>
12+
13+
<p>Select any two <strong>non-overlapping</strong> subtrees. Your <strong>score</strong> is the bitwise XOR of the sum of the values within those subtrees.</p>
14+
15+
<p>Return <em>the</em> <em><strong>maximum</strong></em> <i>possible <strong>score</strong> you can achieve</i>. <em>If it is impossible to find two nonoverlapping subtrees</em>, return <code>0</code>.</p>
16+
17+
<p><strong>Note</strong> that:</p>
18+
19+
<ul>
20+
<li>The <strong>subtree</strong> of a node is the tree consisting of that node and all of its descendants.</li>
21+
<li>Two subtrees are <strong>non-overlapping </strong>if they do not share <strong>any common</strong> node.</li>
22+
</ul>
23+
24+
<p>&nbsp;</p>
25+
<p><strong class="example">Example 1:</strong></p>
26+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2479.Maximum%20XOR%20of%20Two%20Non-Overlapping%20Subtrees/images/treemaxxor.png" style="width: 346px; height: 249px;" />
27+
<pre>
28+
<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,3],[1,4],[2,5]], values = [2,8,3,6,2,5]
29+
<strong>Output:</strong> 24
30+
<strong>Explanation:</strong> Node 1&#39;s subtree has sum of values 16, while node 2&#39;s subtree has sum of values 8, so choosing these nodes will yield a score of 16 XOR 8 = 24. It can be proved that is the maximum possible score we can obtain.
31+
</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2479.Maximum%20XOR%20of%20Two%20Non-Overlapping%20Subtrees/images/tree3drawio.png" style="width: 240px; height: 261px;" />
35+
<pre>
36+
<strong>Input:</strong> n = 3, edges = [[0,1],[1,2]], values = [4,6,1]
37+
<strong>Output:</strong> 0
38+
<strong>Explanation:</strong> There is no possible way to select two non-overlapping subtrees, so we just return 0.
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>2 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
46+
<li><code>edges.length == n - 1</code></li>
47+
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
48+
<li><code>values.length == n</code></li>
49+
<li><code>1 &lt;= values[i] &lt;= 10<sup>9</sup></code></li>
50+
<li>It is guaranteed that <code>edges</code> represents a valid tree.</li>
51+
</ul>
52+
53+
## 解法
54+
55+
<!-- 这里可写通用的实现逻辑 -->
56+
57+
<!-- tabs:start -->
58+
59+
### **Python3**
60+
61+
<!-- 这里可写当前语言的特殊实现逻辑 -->
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```java
72+
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
79+
```
80+
81+
### **Go**
82+
83+
```go
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->

0 commit comments

Comments
 (0)