Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2872f88

Browse files
authored
feat: add weekly contest 433 and biweekly contest 148 (doocs#3970)
1 parent a9dbf34 commit 2872f88

File tree

50 files changed

+2922
-49
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2922
-49
lines changed

solution/0200-0299/0238.Product of Array Except Self/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tags:
3737
<ul>
3838
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
3939
<li><code>-30 &lt;= nums[i] &lt;= 30</code></li>
40-
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
40+
<li>The input is generated such that <code>answer[i]</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
4141
</ul>
4242

4343
<p>&nbsp;</p>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ tags:
2525

2626
<p>&nbsp;</p>
2727
<p><strong class="example">Example 1:</strong></p>
28-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0554.Brick%20Wall/images/cutwall-grid.jpg" style="width: 493px; height: 577px;" />
28+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0554.Brick%20Wall/images/a.png" style="width: 400px; height: 384px;" />
2929
<pre>
3030
<strong>Input:</strong> wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
3131
<strong>Output:</strong> 2

solution/1400-1499/1400.Construct K Palindrome Strings/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tags:
2121

2222
<!-- description:start -->
2323

24-
<p>Given a string <code>s</code> and an integer <code>k</code>, return <code>true</code> if you can use all the characters in <code>s</code> to construct <code>k</code> <span data-keyword="palindrome-string">palindrome strings</span> or <code>false</code> otherwise.</p>
24+
<p>Given a string <code>s</code> and an integer <code>k</code>, return <code>true</code> if you can use all the characters in <code>s</code> to construct <strong>non-empty</strong> <code>k</code> <span data-keyword="palindrome-string">palindrome strings</span> or <code>false</code> otherwise.</p>
2525

2626
<p>&nbsp;</p>
2727
<p><strong class="example">Example 1:</strong></p>

solution/2200-2299/2266.Count Number of Texts/README.md

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,32 @@ Alice 可能发出的文字信息包括:
8080

8181
<!-- solution:start -->
8282

83-
### 方法一
83+
### 方法一:分组 + 动态规划
84+
85+
根据题目描述,对于字符串 $\textit{pressedKeys}$ 中连续的相同字符,可以将其分为一组,然后分别计算每组的方案数,最后将所有组的方案数相乘即可。
86+
87+
问题的关键在于如何计算每组的方案数。
88+
89+
如果一组字符为 '7' 或 '9',我们可以分别将该组的末尾 $1$, $2$, $3$, $4$ 个字符视为一个字母,然后将该组字符规模缩小,转化为规模更小的子问题。
90+
91+
同样地,如果一组字符为 '2', '3', '4', '5', '6', '8',我们可以将该组的末尾 $1$, $2$, $3$ 个字符视为一个字母,然后将该组字符规模缩小,转化为规模更小的子问题。
92+
93+
因此,我们定义 $f[i]$ 表示长度为 $i$ 的连续相同字符,且字符不为 '7' 或 '9' 的方案数,定义 $g[i]$ 表示长度为 $i$ 的连续相同字符,且字符为 '7' 或 '9' 的方案数。
94+
95+
初始时 $f[0] = f[1] = 1$, $f[2] = 2$, $f[3] = 4$, $g[0] = g[1] = 1$, $g[2] = 2$, $g[3] = 4$。
96+
97+
对于 $i \ge 4$,有:
98+
99+
$$
100+
\begin{aligned}
101+
f[i] & = f[i-1] + f[i-2] + f[i-3] \\
102+
g[i] & = g[i-1] + g[i-2] + g[i-3] + g[i-4]
103+
\end{aligned}
104+
$$
105+
106+
最后,我们遍历 $\textit{pressedKeys}$,将连续相同字符分组,然后计算每组的方案数,最后将所有组的方案数相乘即可。
107+
108+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $\textit{pressedKeys}$ 的长度。
84109

85110
<!-- tabs:start -->
86111

@@ -98,9 +123,9 @@ for _ in range(100000):
98123
class Solution:
99124
def countTexts(self, pressedKeys: str) -> int:
100125
ans = 1
101-
for ch, s in groupby(pressedKeys):
126+
for c, s in groupby(pressedKeys):
102127
m = len(list(s))
103-
ans = ans * (g[m] if ch in "79" else f[m]) % mod
128+
ans = ans * (g[m] if c in "79" else f[m]) % mod
104129
return ans
105130
```
106131

@@ -113,12 +138,10 @@ class Solution {
113138
private static long[] f = new long[N];
114139
private static long[] g = new long[N];
115140
static {
116-
f[0] = 1;
117-
f[1] = 1;
141+
f[0] = f[1] = 1;
118142
f[2] = 2;
119143
f[3] = 4;
120-
g[0] = 1;
121-
g[1] = 1;
144+
g[0] = g[1] = 1;
122145
g[2] = 2;
123146
g[3] = 4;
124147
for (int i = 4; i < N; ++i) {
@@ -130,10 +153,11 @@ class Solution {
130153
public int countTexts(String pressedKeys) {
131154
long ans = 1;
132155
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
133-
int j = i;
134156
char c = pressedKeys.charAt(i);
135-
for (; j + 1 < n && pressedKeys.charAt(j + 1) == c; ++j)
136-
;
157+
int j = i;
158+
while (j + 1 < n && pressedKeys.charAt(j + 1) == c) {
159+
++j;
160+
}
137161
int cnt = j - i + 1;
138162
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
139163
ans %= MOD;
@@ -144,6 +168,45 @@ class Solution {
144168
}
145169
```
146170

171+
#### C++
172+
173+
```cpp
174+
const int mod = 1e9 + 7;
175+
const int n = 1e5 + 10;
176+
long long f[n], g[n];
177+
178+
int init = []() {
179+
f[0] = g[0] = 1;
180+
f[1] = g[1] = 1;
181+
f[2] = g[2] = 2;
182+
f[3] = g[3] = 4;
183+
for (int i = 4; i < n; ++i) {
184+
f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % mod;
185+
g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % mod;
186+
}
187+
return 0;
188+
}();
189+
190+
class Solution {
191+
public:
192+
int countTexts(string pressedKeys) {
193+
long long ans = 1;
194+
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
195+
char c = pressedKeys[i];
196+
int j = i;
197+
while (j + 1 < n && pressedKeys[j + 1] == c) {
198+
++j;
199+
}
200+
int cnt = j - i + 1;
201+
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
202+
ans %= mod;
203+
i = j;
204+
}
205+
return ans;
206+
}
207+
};
208+
```
209+
147210
#### Go
148211
149212
```go

solution/2200-2299/2266.Count Number of Texts/README_EN.md

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,32 @@ Since we need to return the answer modulo 10<sup>9</sup> + 7, we return 20828761
7676

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

79-
### Solution 1
79+
### Solution 1: Grouping + Dynamic Programming
80+
81+
According to the problem description, for consecutive identical characters in the string $\textit{pressedKeys}$, we can group them together and then calculate the number of ways for each group. Finally, we multiply the number of ways for all groups.
82+
83+
The key problem is how to calculate the number of ways for each group.
84+
85+
If a group of characters is '7' or '9', we can consider the last $1$, $2$, $3$, or $4$ characters of the group as one letter, then reduce the size of the group and transform it into a smaller subproblem.
86+
87+
Similarly, if a group of characters is '2', '3', '4', '5', '6', or '8', we can consider the last $1$, $2$, or $3$ characters of the group as one letter, then reduce the size of the group and transform it into a smaller subproblem.
88+
89+
Therefore, we define $f[i]$ to represent the number of ways for a group of length $i$ with identical characters that are not '7' or '9', and $g[i]$ to represent the number of ways for a group of length $i$ with identical characters that are '7' or '9'.
90+
91+
Initially, $f[0] = f[1] = 1$, $f[2] = 2$, $f[3] = 4$, $g[0] = g[1] = 1$, $g[2] = 2$, $g[3] = 4$.
92+
93+
For $i \ge 4$, we have:
94+
95+
$$
96+
\begin{aligned}
97+
f[i] & = f[i-1] + f[i-2] + f[i-3] \\
98+
g[i] & = g[i-1] + g[i-2] + g[i-3] + g[i-4]
99+
\end{aligned}
100+
$$
101+
102+
Finally, we traverse $\textit{pressedKeys}$, group consecutive identical characters, calculate the number of ways for each group, and multiply the number of ways for all groups.
103+
104+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $\textit{pressedKeys}$.
80105

81106
<!-- tabs:start -->
82107

@@ -94,9 +119,9 @@ for _ in range(100000):
94119
class Solution:
95120
def countTexts(self, pressedKeys: str) -> int:
96121
ans = 1
97-
for ch, s in groupby(pressedKeys):
122+
for c, s in groupby(pressedKeys):
98123
m = len(list(s))
99-
ans = ans * (g[m] if ch in "79" else f[m]) % mod
124+
ans = ans * (g[m] if c in "79" else f[m]) % mod
100125
return ans
101126
```
102127

@@ -109,12 +134,10 @@ class Solution {
109134
private static long[] f = new long[N];
110135
private static long[] g = new long[N];
111136
static {
112-
f[0] = 1;
113-
f[1] = 1;
137+
f[0] = f[1] = 1;
114138
f[2] = 2;
115139
f[3] = 4;
116-
g[0] = 1;
117-
g[1] = 1;
140+
g[0] = g[1] = 1;
118141
g[2] = 2;
119142
g[3] = 4;
120143
for (int i = 4; i < N; ++i) {
@@ -126,10 +149,11 @@ class Solution {
126149
public int countTexts(String pressedKeys) {
127150
long ans = 1;
128151
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
129-
int j = i;
130152
char c = pressedKeys.charAt(i);
131-
for (; j + 1 < n && pressedKeys.charAt(j + 1) == c; ++j)
132-
;
153+
int j = i;
154+
while (j + 1 < n && pressedKeys.charAt(j + 1) == c) {
155+
++j;
156+
}
133157
int cnt = j - i + 1;
134158
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
135159
ans %= MOD;
@@ -140,6 +164,45 @@ class Solution {
140164
}
141165
```
142166

167+
#### C++
168+
169+
```cpp
170+
const int mod = 1e9 + 7;
171+
const int n = 1e5 + 10;
172+
long long f[n], g[n];
173+
174+
int init = []() {
175+
f[0] = g[0] = 1;
176+
f[1] = g[1] = 1;
177+
f[2] = g[2] = 2;
178+
f[3] = g[3] = 4;
179+
for (int i = 4; i < n; ++i) {
180+
f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % mod;
181+
g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % mod;
182+
}
183+
return 0;
184+
}();
185+
186+
class Solution {
187+
public:
188+
int countTexts(string pressedKeys) {
189+
long long ans = 1;
190+
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
191+
char c = pressedKeys[i];
192+
int j = i;
193+
while (j + 1 < n && pressedKeys[j + 1] == c) {
194+
++j;
195+
}
196+
int cnt = j - i + 1;
197+
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
198+
ans %= mod;
199+
i = j;
200+
}
201+
return ans;
202+
}
203+
};
204+
```
205+
143206
#### Go
144207
145208
```go
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const int mod = 1e9 + 7;
2+
const int n = 1e5 + 10;
3+
long long f[n], g[n];
4+
5+
int init = []() {
6+
f[0] = g[0] = 1;
7+
f[1] = g[1] = 1;
8+
f[2] = g[2] = 2;
9+
f[3] = g[3] = 4;
10+
for (int i = 4; i < n; ++i) {
11+
f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % mod;
12+
g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % mod;
13+
}
14+
return 0;
15+
}();
16+
17+
class Solution {
18+
public:
19+
int countTexts(string pressedKeys) {
20+
long long ans = 1;
21+
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
22+
char c = pressedKeys[i];
23+
int j = i;
24+
while (j + 1 < n && pressedKeys[j + 1] == c) {
25+
++j;
26+
}
27+
int cnt = j - i + 1;
28+
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
29+
ans %= mod;
30+
i = j;
31+
}
32+
return ans;
33+
}
34+
};

solution/2200-2299/2266.Count Number of Texts/Solution.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ class Solution {
44
private static long[] f = new long[N];
55
private static long[] g = new long[N];
66
static {
7-
f[0] = 1;
8-
f[1] = 1;
7+
f[0] = f[1] = 1;
98
f[2] = 2;
109
f[3] = 4;
11-
g[0] = 1;
12-
g[1] = 1;
10+
g[0] = g[1] = 1;
1311
g[2] = 2;
1412
g[3] = 4;
1513
for (int i = 4; i < N; ++i) {
@@ -21,15 +19,16 @@ class Solution {
2119
public int countTexts(String pressedKeys) {
2220
long ans = 1;
2321
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
24-
int j = i;
2522
char c = pressedKeys.charAt(i);
26-
for (; j + 1 < n && pressedKeys.charAt(j + 1) == c; ++j)
27-
;
23+
int j = i;
24+
while (j + 1 < n && pressedKeys.charAt(j + 1) == c) {
25+
++j;
26+
}
2827
int cnt = j - i + 1;
2928
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
3029
ans %= MOD;
3130
i = j;
3231
}
3332
return (int) ans;
3433
}
35-
}
34+
}

solution/2200-2299/2266.Count Number of Texts/Solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Solution:
1010
def countTexts(self, pressedKeys: str) -> int:
1111
ans = 1
12-
for ch, s in groupby(pressedKeys):
12+
for c, s in groupby(pressedKeys):
1313
m = len(list(s))
14-
ans = ans * (g[m] if ch in "79" else f[m]) % mod
14+
ans = ans * (g[m] if c in "79" else f[m]) % mod
1515
return ans

solution/3400-3499/3421.Find Students Who Improved/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ tags:
3434
<p>编写一个解决方案来查找 <strong>进步的学生</strong>。如果 <strong>同时</strong> 满足以下两个条件,则该学生被认为是进步的:</p>
3535

3636
<ul>
37-
<li>在 <strong>同一科目</strong>&nbsp;至少有两个不同日期的考试。</li>
38-
<li>他们在该学科&nbsp;<strong>最近的分数</strong>&nbsp;比他们 <strong>第一次的分数更高。</strong></li>
37+
<li>在 <strong>同一科目</strong> 至少参加过两个不同日期的考试。</li>
38+
<li>他们在该学科<strong> 最近的分数 </strong>比他们 第一次该学科考试的分数更高。</li>
3939
</ul>
4040

4141
<p>返回结果表以&nbsp;<code>student_id</code>,<code>subject</code> <strong>升序</strong>&nbsp;排序。</p>

solution/3400-3499/3421.Find Students Who Improved/README_EN.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ Each row contains information about a student&#39;s score in a specific subject
5454
+------------+----------+-------+------------+
5555
| student_id | subject | score | exam_date |
5656
+------------+----------+-------+------------+
57-
| 101 | Math | 70 | 15-01-2023 |
58-
| 101 | Math | 85 | 15-02-2023 |
59-
| 101 | Physics | 65 | 15-01-2023 |
60-
| 101 | Physics | 60 | 15-02-2023 |
61-
| 102 | Math | 80 | 15-01-2023 |
62-
| 102 | Math | 85 | 15-02-2023 |
63-
| 103 | Math | 90 | 15-01-2023 |
64-
| 104 | Physics | 75 | 15-01-2023 |
65-
| 104 | Physics | 85 | 15-02-2023 |
57+
| 101 | Math | 70 | 2023-01-15 |
58+
| 101 | Math | 85 | 2023-02-15 |
59+
| 101 | Physics | 65 | 2023-01-15 |
60+
| 101 | Physics | 60 | 2023-02-15 |
61+
| 102 | Math | 80 | 2023-01-15 |
62+
| 102 | Math | 85 | 2023-02-15 |
63+
| 103 | Math | 90 | 2023-01-15 |
64+
| 104 | Physics | 75 | 2023-01-15 |
65+
| 104 | Physics | 85 | 2023-02-15 |
6666
+------------+----------+-------+------------+
6767
</pre>
6868

0 commit comments

Comments
 (0)