Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a6a47a

Browse files
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‎

Lines changed: 123 additions & 1 deletion
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‎

Lines changed: 105 additions & 1 deletion
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
### **...**
Lines changed: 23 additions & 0 deletions
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+
};
Lines changed: 19 additions & 0 deletions
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+
}
Lines changed: 22 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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
Lines changed: 19 additions & 0 deletions
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‎

Lines changed: 10 additions & 0 deletions
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‎

Lines changed: 10 additions & 0 deletions
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**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /