Skip to content

Commit

Permalink
feat: add weekly contest 433 and biweekly contest 148
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Jan 19, 2025
1 parent a9dbf34 commit 572bdda
Show file tree
Hide file tree
Showing 50 changed files with 2,922 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tags:
<ul>
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-30 &lt;= nums[i] &lt;= 30</code></li>
<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>
<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>
</ul>

<p>&nbsp;</p>
Expand Down
2 changes: 1 addition & 1 deletion solution/0500-0599/0554.Brick Wall/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tags:

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<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;" />
<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;" />
<pre>
<strong>Input:</strong> wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
<strong>Output:</strong> 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tags:

<!-- description:start -->

<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>
<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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
Expand Down
83 changes: 73 additions & 10 deletions solution/2200-2299/2266.Count Number of Texts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,32 @@ Alice 可能发出的文字信息包括:

<!-- solution:start -->

### 方法一
### 方法一:分组 + 动态规划

根据题目描述,对于字符串 $\textit{pressedKeys}$ 中连续的相同字符,可以将其分为一组,然后分别计算每组的方案数,最后将所有组的方案数相乘即可。

问题的关键在于如何计算每组的方案数。

如果一组字符为 '7' 或 '9',我们可以分别将该组的末尾 $1$, $2$, $3$, $4$ 个字符视为一个字母,然后将该组字符规模缩小,转化为规模更小的子问题。

同样地,如果一组字符为 '2', '3', '4', '5', '6', '8',我们可以将该组的末尾 $1$, $2$, $3$ 个字符视为一个字母,然后将该组字符规模缩小,转化为规模更小的子问题。

因此,我们定义 $f[i]$ 表示长度为 $i$ 的连续相同字符,且字符不为 '7' 或 '9' 的方案数,定义 $g[i]$ 表示长度为 $i$ 的连续相同字符,且字符为 '7' 或 '9' 的方案数。

初始时 $f[0] = f[1] = 1$, $f[2] = 2$, $f[3] = 4$, $g[0] = g[1] = 1$, $g[2] = 2$, $g[3] = 4$。

对于 $i \ge 4$,有:

$$
\begin{aligned}
f[i] & = f[i-1] + f[i-2] + f[i-3] \\
g[i] & = g[i-1] + g[i-2] + g[i-3] + g[i-4]
\end{aligned}
$$

最后,我们遍历 $\textit{pressedKeys}$,将连续相同字符分组,然后计算每组的方案数,最后将所有组的方案数相乘即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $\textit{pressedKeys}$ 的长度。

<!-- tabs:start -->

Expand All @@ -98,9 +123,9 @@ for _ in range(100000):
class Solution:
def countTexts(self, pressedKeys: str) -> int:
ans = 1
for ch, s in groupby(pressedKeys):
for c, s in groupby(pressedKeys):
m = len(list(s))
ans = ans * (g[m] if ch in "79" else f[m]) % mod
ans = ans * (g[m] if c in "79" else f[m]) % mod
return ans
```

Expand All @@ -113,12 +138,10 @@ class Solution {
private static long[] f = new long[N];
private static long[] g = new long[N];
static {
f[0] = 1;
f[1] = 1;
f[0] = f[1] = 1;
f[2] = 2;
f[3] = 4;
g[0] = 1;
g[1] = 1;
g[0] = g[1] = 1;
g[2] = 2;
g[3] = 4;
for (int i = 4; i < N; ++i) {
Expand All @@ -130,10 +153,11 @@ class Solution {
public int countTexts(String pressedKeys) {
long ans = 1;
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
int j = i;
char c = pressedKeys.charAt(i);
for (; j + 1 < n && pressedKeys.charAt(j + 1) == c; ++j)
;
int j = i;
while (j + 1 < n && pressedKeys.charAt(j + 1) == c) {
++j;
}
int cnt = j - i + 1;
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
ans %= MOD;
Expand All @@ -144,6 +168,45 @@ class Solution {
}
```

#### C++

```cpp
const int mod = 1e9 + 7;
const int n = 1e5 + 10;
long long f[n], g[n];

int init = []() {
f[0] = g[0] = 1;
f[1] = g[1] = 1;
f[2] = g[2] = 2;
f[3] = g[3] = 4;
for (int i = 4; i < n; ++i) {
f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % mod;
g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % mod;
}
return 0;
}();

class Solution {
public:
int countTexts(string pressedKeys) {
long long ans = 1;
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
char c = pressedKeys[i];
int j = i;
while (j + 1 < n && pressedKeys[j + 1] == c) {
++j;
}
int cnt = j - i + 1;
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
ans %= mod;
i = j;
}
return ans;
}
};
```
#### Go
```go
Expand Down
83 changes: 73 additions & 10 deletions solution/2200-2299/2266.Count Number of Texts/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,32 @@ Since we need to return the answer modulo 10<sup>9</sup> + 7, we return 20828761

<!-- solution:start -->

### Solution 1
### Solution 1: Grouping + Dynamic Programming

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.

The key problem is how to calculate the number of ways for each group.

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.

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.

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'.

Initially, $f[0] = f[1] = 1$, $f[2] = 2$, $f[3] = 4$, $g[0] = g[1] = 1$, $g[2] = 2$, $g[3] = 4$.

For $i \ge 4$, we have:

$$
\begin{aligned}
f[i] & = f[i-1] + f[i-2] + f[i-3] \\
g[i] & = g[i-1] + g[i-2] + g[i-3] + g[i-4]
\end{aligned}
$$

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $\textit{pressedKeys}$.

<!-- tabs:start -->

Expand All @@ -94,9 +119,9 @@ for _ in range(100000):
class Solution:
def countTexts(self, pressedKeys: str) -> int:
ans = 1
for ch, s in groupby(pressedKeys):
for c, s in groupby(pressedKeys):
m = len(list(s))
ans = ans * (g[m] if ch in "79" else f[m]) % mod
ans = ans * (g[m] if c in "79" else f[m]) % mod
return ans
```

Expand All @@ -109,12 +134,10 @@ class Solution {
private static long[] f = new long[N];
private static long[] g = new long[N];
static {
f[0] = 1;
f[1] = 1;
f[0] = f[1] = 1;
f[2] = 2;
f[3] = 4;
g[0] = 1;
g[1] = 1;
g[0] = g[1] = 1;
g[2] = 2;
g[3] = 4;
for (int i = 4; i < N; ++i) {
Expand All @@ -126,10 +149,11 @@ class Solution {
public int countTexts(String pressedKeys) {
long ans = 1;
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
int j = i;
char c = pressedKeys.charAt(i);
for (; j + 1 < n && pressedKeys.charAt(j + 1) == c; ++j)
;
int j = i;
while (j + 1 < n && pressedKeys.charAt(j + 1) == c) {
++j;
}
int cnt = j - i + 1;
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
ans %= MOD;
Expand All @@ -140,6 +164,45 @@ class Solution {
}
```

#### C++

```cpp
const int mod = 1e9 + 7;
const int n = 1e5 + 10;
long long f[n], g[n];

int init = []() {
f[0] = g[0] = 1;
f[1] = g[1] = 1;
f[2] = g[2] = 2;
f[3] = g[3] = 4;
for (int i = 4; i < n; ++i) {
f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % mod;
g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % mod;
}
return 0;
}();

class Solution {
public:
int countTexts(string pressedKeys) {
long long ans = 1;
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
char c = pressedKeys[i];
int j = i;
while (j + 1 < n && pressedKeys[j + 1] == c) {
++j;
}
int cnt = j - i + 1;
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
ans %= mod;
i = j;
}
return ans;
}
};
```
#### Go
```go
Expand Down
34 changes: 34 additions & 0 deletions solution/2200-2299/2266.Count Number of Texts/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const int mod = 1e9 + 7;
const int n = 1e5 + 10;
long long f[n], g[n];

int init = []() {
f[0] = g[0] = 1;
f[1] = g[1] = 1;
f[2] = g[2] = 2;
f[3] = g[3] = 4;
for (int i = 4; i < n; ++i) {
f[i] = (f[i - 1] + f[i - 2] + f[i - 3]) % mod;
g[i] = (g[i - 1] + g[i - 2] + g[i - 3] + g[i - 4]) % mod;
}
return 0;
}();

class Solution {
public:
int countTexts(string pressedKeys) {
long long ans = 1;
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
char c = pressedKeys[i];
int j = i;
while (j + 1 < n && pressedKeys[j + 1] == c) {
++j;
}
int cnt = j - i + 1;
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
ans %= mod;
i = j;
}
return ans;
}
};
15 changes: 7 additions & 8 deletions solution/2200-2299/2266.Count Number of Texts/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ class Solution {
private static long[] f = new long[N];
private static long[] g = new long[N];
static {
f[0] = 1;
f[1] = 1;
f[0] = f[1] = 1;
f[2] = 2;
f[3] = 4;
g[0] = 1;
g[1] = 1;
g[0] = g[1] = 1;
g[2] = 2;
g[3] = 4;
for (int i = 4; i < N; ++i) {
Expand All @@ -21,15 +19,16 @@ class Solution {
public int countTexts(String pressedKeys) {
long ans = 1;
for (int i = 0, n = pressedKeys.length(); i < n; ++i) {
int j = i;
char c = pressedKeys.charAt(i);
for (; j + 1 < n && pressedKeys.charAt(j + 1) == c; ++j)
;
int j = i;
while (j + 1 < n && pressedKeys.charAt(j + 1) == c) {
++j;
}
int cnt = j - i + 1;
ans = c == '7' || c == '9' ? ans * g[cnt] : ans * f[cnt];
ans %= MOD;
i = j;
}
return (int) ans;
}
}
}
4 changes: 2 additions & 2 deletions solution/2200-2299/2266.Count Number of Texts/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Solution:
def countTexts(self, pressedKeys: str) -> int:
ans = 1
for ch, s in groupby(pressedKeys):
for c, s in groupby(pressedKeys):
m = len(list(s))
ans = ans * (g[m] if ch in "79" else f[m]) % mod
ans = ans * (g[m] if c in "79" else f[m]) % mod
return ans
4 changes: 2 additions & 2 deletions solution/3400-3499/3421.Find Students Who Improved/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ tags:
<p>编写一个解决方案来查找 <strong>进步的学生</strong>。如果 <strong>同时</strong> 满足以下两个条件,则该学生被认为是进步的:</p>

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

<p>返回结果表以&nbsp;<code>student_id</code>,<code>subject</code> <strong>升序</strong>&nbsp;排序。</p>
Expand Down
18 changes: 9 additions & 9 deletions solution/3400-3499/3421.Find Students Who Improved/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ Each row contains information about a student&#39;s score in a specific subject
+------------+----------+-------+------------+
| student_id | subject | score | exam_date |
+------------+----------+-------+------------+
| 101 | Math | 70 | 15-01-2023 |
| 101 | Math | 85 | 15-02-2023 |
| 101 | Physics | 65 | 15-01-2023 |
| 101 | Physics | 60 | 15-02-2023 |
| 102 | Math | 80 | 15-01-2023 |
| 102 | Math | 85 | 15-02-2023 |
| 103 | Math | 90 | 15-01-2023 |
| 104 | Physics | 75 | 15-01-2023 |
| 104 | Physics | 85 | 15-02-2023 |
| 101 | Math | 70 | 2023-01-15 |
| 101 | Math | 85 | 2023-02-15 |
| 101 | Physics | 65 | 2023-01-15 |
| 101 | Physics | 60 | 2023-02-15 |
| 102 | Math | 80 | 2023-01-15 |
| 102 | Math | 85 | 2023-02-15 |
| 103 | Math | 90 | 2023-01-15 |
| 104 | Physics | 75 | 2023-01-15 |
| 104 | Physics | 85 | 2023-02-15 |
+------------+----------+-------+------------+
</pre>

Expand Down
Loading

0 comments on commit 572bdda

Please sign in to comment.