Skip to content

Commit 0a6a47a

Browse files
authoredJul 15, 2023
feat: add solutions to lc problem: No.1927 (doocs#1217)
No.1927.Sum Game
1 parent 2b84ac0 commit 0a6a47a

File tree

12 files changed

+368
-3
lines changed

12 files changed

+368
-3
lines changed
 

‎solution/1900-1999/1927.Sum Game/README.md

+123-1
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,144 @@ Bob 获胜,因为 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7 。
7171

7272
<!-- 这里可写通用的实现逻辑 -->
7373

74+
**方法一:分类讨论**
75+
76+
如果 `'?'` 的个数为奇数,那么 Alice 一定会获胜,因为她可以选择将最后一个 `'?'` 替换为任何一个数字,使得前一半的和与后一半的和不相等。
77+
78+
如果 `'?'` 的个数为偶数,Alice 为了使得前一半的和与后一半的和不相等,那么会选择在当前和较大的一半数字中放置 $9$,在当前和较小的一半数字中放置 $0$,而 Bob 为了使得前后两半的和相等,那么会选择在 Alice 替换数字的另一半放置一个与 Alice 替换数字相同的数字。
79+
80+
因此,最终会使得剩下的所有偶数个 `'?'` 集中在其中一半。假设当前两半的数字差值为 $d$。
81+
82+
我们先考虑,如果剩下两个 `'?'`,差值为 $x$,此时:
83+
84+
- 如果 $x \lt 9$,那么 Alice 必胜,因为 Alice 可以将其中一个 `'?'` 替换为 $9$,使得前一半的和与后一半的和不相等;
85+
- 如果 $x \gt 9$,那么 Alice 必胜,因为 Alice 可以将其中一个 `'?'` 替换为 $0$,使得前一半的和与后一半的和不相等;
86+
- 如果 $x = 9$,那么 Bob 必胜,假设 Alice 替换的数字为 $a$,那么 Bob 可以将另一个 `'?'` 替换为 $9 - a$,使得前后两半的和相等。
87+
88+
因此,如果两半数字差值为 $d= \frac{9 \times cnt}{2}$,其中 $cnt$ 为剩下的 `'?'` 的个数,那么 Bob 必胜,否则 Alice 必胜。
89+
90+
时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。空间复杂度 $O(1)$。
91+
7492
<!-- tabs:start -->
7593

7694
### **Python3**
7795

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

8098
```python
81-
99+
class Solution:
100+
def sumGame(self, num: str) -> bool:
101+
n = len(num)
102+
cnt1 = num[: n // 2].count("?")
103+
cnt2 = num[n // 2 :].count("?")
104+
s1 = sum(int(x) for x in num[: n // 2] if x != "?")
105+
s2 = sum(int(x) for x in num[n // 2 :] if x != "?")
106+
return (cnt1 + cnt2) % 2 == 1 or s1 - s2 != 9 * (cnt2 - cnt1) // 2
82107
```
83108

84109
### **Java**
85110

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

88113
```java
114+
class Solution {
115+
public boolean sumGame(String num) {
116+
int n = num.length();
117+
int cnt1 = 0, cnt2 = 0;
118+
int s1 = 0, s2 = 0;
119+
for (int i = 0; i < n / 2; ++i) {
120+
if (num.charAt(i) == '?') {
121+
cnt1++;
122+
} else {
123+
s1 += num.charAt(i) - '0';
124+
}
125+
}
126+
for (int i = n / 2; i < n; ++i) {
127+
if (num.charAt(i) == '?') {
128+
cnt2++;
129+
} else {
130+
s2 += num.charAt(i) - '0';
131+
}
132+
}
133+
return (cnt1 + cnt2) % 2 == 1 || s1 - s2 != 9 * (cnt2 - cnt1) / 2;
134+
}
135+
}
136+
```
137+
138+
### **C++**
139+
140+
```cpp
141+
class Solution {
142+
public:
143+
bool sumGame(string num) {
144+
int n = num.size();
145+
int cnt1 = 0, cnt2 = 0;
146+
int s1 = 0, s2 = 0;
147+
for (int i = 0; i < n / 2; ++i) {
148+
if (num[i] == '?') {
149+
cnt1++;
150+
} else {
151+
s1 += num[i] - '0';
152+
}
153+
}
154+
for (int i = n / 2; i < n; ++i) {
155+
if (num[i] == '?') {
156+
cnt2++;
157+
} else {
158+
s2 += num[i] - '0';
159+
}
160+
}
161+
return (cnt1 + cnt2) % 2 == 1 || (s1 - s2) != 9 * (cnt2 - cnt1) / 2;
162+
}
163+
};
164+
```
165+
166+
### **Go**
167+
168+
```go
169+
func sumGame(num string) bool {
170+
n := len(num)
171+
var cnt1, cnt2, s1, s2 int
172+
for i := 0; i < n/2; i++ {
173+
if num[i] == '?' {
174+
cnt1++
175+
} else {
176+
s1 += int(num[i] - '0')
177+
}
178+
}
179+
for i := n / 2; i < n; i++ {
180+
if num[i] == '?' {
181+
cnt2++
182+
} else {
183+
s2 += int(num[i] - '0')
184+
}
185+
}
186+
return (cnt1+cnt2)%2 == 1 || s1-s2 != (cnt2-cnt1)*9/2
187+
}
188+
```
89189

190+
### **TypeScript**
191+
192+
```ts
193+
function sumGame(num: string): boolean {
194+
const n = num.length;
195+
let [cnt1, cnt2, s1, s2] = [0, 0, 0, 0];
196+
for (let i = 0; i < n >> 1; ++i) {
197+
if (num[i] === '?') {
198+
++cnt1;
199+
} else {
200+
s1 += num[i].charCodeAt(0) - '0'.charCodeAt(0);
201+
}
202+
}
203+
for (let i = n >> 1; i < n; ++i) {
204+
if (num[i] === '?') {
205+
++cnt2;
206+
} else {
207+
s2 += num[i].charCodeAt(0) - '0'.charCodeAt(0);
208+
}
209+
}
210+
return (cnt1 + cnt2) % 2 === 1 || 2 * (s1 - s2) !== 9 * (cnt2 - cnt1);
211+
}
90212
```
91213

92214
### **...**

‎solution/1900-1999/1927.Sum Game/README_EN.md

+105-1
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,117 @@ Bob wins because 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7.
7070
### **Python3**
7171

7272
```python
73-
73+
class Solution:
74+
def sumGame(self, num: str) -> bool:
75+
n = len(num)
76+
cnt1 = num[: n // 2].count("?")
77+
cnt2 = num[n // 2 :].count("?")
78+
s1 = sum(int(x) for x in num[: n // 2] if x != "?")
79+
s2 = sum(int(x) for x in num[n // 2 :] if x != "?")
80+
return (cnt1 + cnt2) % 2 == 1 or s1 - s2 != 9 * (cnt2 - cnt1) // 2
7481
```
7582

7683
### **Java**
7784

7885
```java
86+
class Solution {
87+
public boolean sumGame(String num) {
88+
int n = num.length();
89+
int cnt1 = 0, cnt2 = 0;
90+
int s1 = 0, s2 = 0;
91+
for (int i = 0; i < n / 2; ++i) {
92+
if (num.charAt(i) == '?') {
93+
cnt1++;
94+
} else {
95+
s1 += num.charAt(i) - '0';
96+
}
97+
}
98+
for (int i = n / 2; i < n; ++i) {
99+
if (num.charAt(i) == '?') {
100+
cnt2++;
101+
} else {
102+
s2 += num.charAt(i) - '0';
103+
}
104+
}
105+
return (cnt1 + cnt2) % 2 == 1 || s1 - s2 != 9 * (cnt2 - cnt1) / 2;
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
bool sumGame(string num) {
116+
int n = num.size();
117+
int cnt1 = 0, cnt2 = 0;
118+
int s1 = 0, s2 = 0;
119+
for (int i = 0; i < n / 2; ++i) {
120+
if (num[i] == '?') {
121+
cnt1++;
122+
} else {
123+
s1 += num[i] - '0';
124+
}
125+
}
126+
for (int i = n / 2; i < n; ++i) {
127+
if (num[i] == '?') {
128+
cnt2++;
129+
} else {
130+
s2 += num[i] - '0';
131+
}
132+
}
133+
return (cnt1 + cnt2) % 2 == 1 || (s1 - s2) != 9 * (cnt2 - cnt1) / 2;
134+
}
135+
};
136+
```
137+
138+
### **Go**
139+
140+
```go
141+
func sumGame(num string) bool {
142+
n := len(num)
143+
var cnt1, cnt2, s1, s2 int
144+
for i := 0; i < n/2; i++ {
145+
if num[i] == '?' {
146+
cnt1++
147+
} else {
148+
s1 += int(num[i] - '0')
149+
}
150+
}
151+
for i := n / 2; i < n; i++ {
152+
if num[i] == '?' {
153+
cnt2++
154+
} else {
155+
s2 += int(num[i] - '0')
156+
}
157+
}
158+
return (cnt1+cnt2)%2 == 1 || s1-s2 != (cnt2-cnt1)*9/2
159+
}
160+
```
79161

162+
### **TypeScript**
163+
164+
```ts
165+
function sumGame(num: string): boolean {
166+
const n = num.length;
167+
let [cnt1, cnt2, s1, s2] = [0, 0, 0, 0];
168+
for (let i = 0; i < n >> 1; ++i) {
169+
if (num[i] === '?') {
170+
++cnt1;
171+
} else {
172+
s1 += num[i].charCodeAt(0) - '0'.charCodeAt(0);
173+
}
174+
}
175+
for (let i = n >> 1; i < n; ++i) {
176+
if (num[i] === '?') {
177+
++cnt2;
178+
} else {
179+
s2 += num[i].charCodeAt(0) - '0'.charCodeAt(0);
180+
}
181+
}
182+
return (cnt1 + cnt2) % 2 === 1 || 2 * (s1 - s2) !== 9 * (cnt2 - cnt1);
183+
}
80184
```
81185

82186
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool sumGame(string num) {
4+
int n = num.size();
5+
int cnt1 = 0, cnt2 = 0;
6+
int s1 = 0, s2 = 0;
7+
for (int i = 0; i < n / 2; ++i) {
8+
if (num[i] == '?') {
9+
cnt1++;
10+
} else {
11+
s1 += num[i] - '0';
12+
}
13+
}
14+
for (int i = n / 2; i < n; ++i) {
15+
if (num[i] == '?') {
16+
cnt2++;
17+
} else {
18+
s2 += num[i] - '0';
19+
}
20+
}
21+
return (cnt1 + cnt2) % 2 == 1 || (s1 - s2) != 9 * (cnt2 - cnt1) / 2;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func sumGame(num string) bool {
2+
n := len(num)
3+
var cnt1, cnt2, s1, s2 int
4+
for i := 0; i < n/2; i++ {
5+
if num[i] == '?' {
6+
cnt1++
7+
} else {
8+
s1 += int(num[i] - '0')
9+
}
10+
}
11+
for i := n / 2; i < n; i++ {
12+
if num[i] == '?' {
13+
cnt2++
14+
} else {
15+
s2 += int(num[i] - '0')
16+
}
17+
}
18+
return (cnt1+cnt2)%2 == 1 || s1-s2 != (cnt2-cnt1)*9/2
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public boolean sumGame(String num) {
3+
int n = num.length();
4+
int cnt1 = 0, cnt2 = 0;
5+
int s1 = 0, s2 = 0;
6+
for (int i = 0; i < n / 2; ++i) {
7+
if (num.charAt(i) == '?') {
8+
cnt1++;
9+
} else {
10+
s1 += num.charAt(i) - '0';
11+
}
12+
}
13+
for (int i = n / 2; i < n; ++i) {
14+
if (num.charAt(i) == '?') {
15+
cnt2++;
16+
} else {
17+
s2 += num.charAt(i) - '0';
18+
}
19+
}
20+
return (cnt1 + cnt2) % 2 == 1 || s1 - s2 != 9 * (cnt2 - cnt1) / 2;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def sumGame(self, num: str) -> bool:
3+
n = len(num)
4+
cnt1 = num[: n // 2].count("?")
5+
cnt2 = num[n // 2 :].count("?")
6+
s1 = sum(int(x) for x in num[: n // 2] if x != "?")
7+
s2 = sum(int(x) for x in num[n // 2 :] if x != "?")
8+
return (cnt1 + cnt2) % 2 == 1 or s1 - s2 != 9 * (cnt2 - cnt1) // 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function sumGame(num: string): boolean {
2+
const n = num.length;
3+
let [cnt1, cnt2, s1, s2] = [0, 0, 0, 0];
4+
for (let i = 0; i < n >> 1; ++i) {
5+
if (num[i] === '?') {
6+
++cnt1;
7+
} else {
8+
s1 += num[i].charCodeAt(0) - '0'.charCodeAt(0);
9+
}
10+
}
11+
for (let i = n >> 1; i < n; ++i) {
12+
if (num[i] === '?') {
13+
++cnt2;
14+
} else {
15+
s2 += num[i].charCodeAt(0) - '0'.charCodeAt(0);
16+
}
17+
}
18+
return (cnt1 + cnt2) % 2 === 1 || 2 * (s1 - s2) !== 9 * (cnt2 - cnt1);
19+
}

‎solution/2700-2799/2761.Prime Pairs With Target Sum/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ These pairs are [3,7] and [5,5], and we return them in the sorted order as descr
4343

4444
## Solutions
4545

46+
**Solution 1: Preprocessing + Enumeration**
47+
48+
First, we pre-process all the prime numbers within the range of $n$, and record them in the array $primes$, where $primes[i]$ is `true` if $i$ is a prime number.
49+
50+
Next, we enumerate $x$ in the range of $[2, \frac{n}{2}]$. In this case, $y = n - x$. If both $primes[x]$ and $primes[y]$ are `true`, then $(x, y)$ is a pair of prime numbers, which is added to the answer.
51+
52+
After the enumeration is complete, we return the answer.
53+
54+
The time complexity is $O(n \log \log n)$ and the space complexity is $O(n)$, where $n$ is the number given in the problem.
55+
4656
<!-- tabs:start -->
4757

4858
### **Python3**

‎solution/2700-2799/2762.Continuous Subarrays/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ Total continuous subarrays = 3 + 2 + 1 = 6.
5353

5454
## Solutions
5555

56+
**Solution 1: Ordered List + Two Pointers**
57+
58+
We can use two pointers, $i$ and $j$, to maintain the left and right endpoints of the current subarray, and use an ordered list to maintain all elements in the current subarray.
59+
60+
Iterate through the array $nums$. For the current number $nums[i]$ we're iterating over, we add it to the ordered list. If the difference between the maximum and minimum values in the ordered list is greater than $2$, we then loop to move the pointer $i$ to the right, continuously removing $nums[i]$ from the ordered list, until the list is empty or the maximum difference between elements in the ordered list is not greater than $2$. At this point, the number of uninterrupted subarrays is $j - i + 1$, which we add to the answer.
61+
62+
After the iteration, return the answer.
63+
64+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
65+
5666
<!-- tabs:start -->
5767

5868
### **Python3**

‎solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ The imbalance number of all other subarrays is 0. Hence, the sum of imbalance nu
5555

5656
## Solutions
5757

58+
**Solution 1: Enumeration + Ordered Set**
59+
60+
We can first enumerate the left endpoint $i$ of the subarray. For each $i$, we enumerate the right endpoint $j$ of the subarray from small to large, and maintain all the elements in the current subarray with an ordered list. We also use a variable $cnt$ to maintain the unbalanced number of the current subarray.
61+
62+
For each number $nums[j]$, we find the first element $nums[k]$ in the ordered list that is greater than or equal to $nums[j]$, and the last element $nums[h]$ that is less than $nums[j]$:
63+
64+
- If $nums[k]$ exists, and the difference between $nums[k]$ and $nums[j]$ is more than $1$, the unbalanced number increases by $1$;
65+
- If $nums[h]$ exists, and the difference between $nums[j]$ and $nums[h]$ is more than $1$, the unbalanced number increases by $1$;
66+
- If both $nums[k]$ and $nums[h]$ exist, then inserting the element $nums[j]$ between $nums[h]$ and $nums[k]$ will reduce the unbalanced number by $1$.
67+
68+
Then, we add the unbalanced number of the current subarray to the answer, and continue the iteration until we finish iterating over all subarrays.
69+
70+
The time complexity is $O(n^2 \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
71+
5872
<!-- tabs:start -->
5973

6074
### **Python3**

‎solution/2700-2799/2764.is Array a Preorder of Some ‌Binary Tree/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ For the preorder traversal, first we visit node 0, then we do the preorder trave
4848

4949
## Solutions
5050

51+
**Solution 1:Depth-First Search**
52+
53+
First, we construct a graph $g$ based on the $nodes$ data, where $g[i]$ represents all the child nodes of node $i$.
54+
55+
Next, we design a function $dfs(i)$, which represents a pre-order traversal starting from node $i$. We use a variable $k$ to represent the $k$-th node in the $nodes$ list that we have currently traversed, with an initial value of $k = 0$.
56+
57+
The execution logic of the function $dfs(i)$ is as follows:
58+
59+
If $i \neq nodes[k][0]$, it indicates that the current sequence is not a pre-order traversal sequence of a binary tree, and returns false.
60+
Otherwise, we increment $k$ by $1$, and then recursively search all child nodes of $i$. If a false is found during the search, we return false immediately. Otherwise, when the search is finished, we return true.
61+
In the main function, we call $dfs(nodes[0][0])$. If the return value is true and $k = |nodes|$, then the $nodes$ sequence is a pre-order traversal sequence of a binary tree, and we return true; otherwise, we return false.
62+
63+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes in $nodes$.
64+
5165
<!-- tabs:start -->
5266

5367
### **Python3**

‎solution/2700-2799/2771.Longest Non-decreasing Subarray From Two Arrays/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Then, we update $f = ff$ and $g = gg$, and update $ans$ to $max(ans, f, g)$.
7272

7373
After the traversal is over, we return $ans$.
7474

75-
Time complexity $O(n)$, where $n$ is the length of the array. Space complexity $O(1)$.
75+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
7676

7777
<!-- tabs:start -->
7878

0 commit comments

Comments
 (0)
Please sign in to comment.