Skip to content

Commit 5cba60b

Browse files
authored
feat: add solutions to lc problems: No.2953,2954 (#2061)
1 parent 14238ec commit 5cba60b

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

solution/1400-1499/1411.Number of Ways to Paint N × 3 Grid/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
**方法一:递推**
6464

65-
把每一行所有可能的状态进行分类。根据对称性原理,当一行只有 $3$ 个元素时,所有合法状态分类为$010$ 型, $012$ 型。
65+
把每一行所有可能的状态进行分类。根据对称性原理,当一行只有 $3$ 个元素时,所有合法状态分类为 $010$ 型以及 $012$ 型。
6666

6767
- 当状态为 $010$ 型时:下一行可能的状态为 $101$, $102$, $121$, $201$, $202$。这 $5$ 个状态可归纳为 $3$ 个 $010$ 型,以及 $2$ 个 $012$ 型。
6868
- 当状态为 $012$ 型时:下一行可能的状态为 $101$, $120$, $121$, $201$。这 $4$ 个状态可归纳为 $2$ 个 $010$ 型,以及 $2$ 个 $012$ 型。

solution/2900-2999/2953.Count Complete Substrings/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:枚举字符种类数 + 滑动窗口**
55+
56+
根据题目描述中的条件 $2$,我们可以发现,一个完全字符串中,相邻两个字符之差不超过 $2$。因此,我们遍历字符串 $word$,可以利用双指针把 $word$ 分割成若干个子字符串,这些子字符串中的字符种类数不超过 $26$,且相邻字符之差不超过 $2$。接下来,我们只需要在每个子字符串中,统计每个字符都出现 $k$ 次的子字符串的个数即可。
57+
58+
我们定义一个函数 $f(s)$,它的功能是统计字符串 $s$ 中每个字符都出现 $k$ 次的子字符串的个数。由于 $s$ 中的字符种类数不超过 $26$,因此我们可以枚举每个字符种类数 $i$,其中 $1 \le i \le 26$,那么每个字符种类数为 $i$ 的子字符串的长度为 $l = i \times k$。
59+
60+
我们可以用一个数组或哈希表 $cnt$ 维护一个长度为 $l$ 的滑动窗口中每个字符出现的次数,用另一个哈希表 $freq$ 维护每个次数出现的次数。如果 $freq[k] = i$,即有 $i$ 个字符都出现了 $k$ 次,那么我们就找到了一个满足条件的子字符串。我们可以用双指针维护这个滑动窗口,每次移动右指针时,我们将右指针指向的字符出现的次数加一,并更新 $freq$ 数组;每次移动左指针时,我们将左指针指向的字符出现的次数减一,并更新 $freq$ 数组。在每次移动指针后,我们都判断 $freq[k]$ 是否等于 $i$,如果等于则说明我们找到了一个满足条件的子字符串。
61+
62+
时间复杂度 $O(n \times |\Sigma|)$,空间复杂度 $O(|\Sigma|)$,其中 $n$ 是字符串 $word$ 的长度;而 $\Sigma$ 是字符集的大小,本题中字符集为小写英文字母,因此 $|\Sigma| = 26$。
63+
5464
<!-- tabs:start -->
5565

5666
### **Python3**

solution/2900-2999/2953.Count Complete Substrings/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545

4646
## Solutions
4747

48+
**Solution 1: Enumerate Character Types + Sliding Window**
49+
50+
According to condition 2 in the problem description, we can find that in a complete string, the difference between two adjacent characters does not exceed 2. Therefore, we traverse the string $word$, and we can use two pointers to split $word$ into several substrings. The number of character types in these substrings does not exceed 26, and the difference between adjacent characters does not exceed 2. Next, we only need to count the number of substrings in each substring where each character appears $k$ times.
51+
52+
We define a function $f(s)$, which is used to count the number of substrings in the string $s$ where each character appears $k$ times. Since the number of character types in $s$ does not exceed 26, we can enumerate each character type $i$, where $1 \le i \le 26$, then the length of the substring with character type $i$ is $l = i \times k$.
53+
54+
We can use an array or hash table $cnt$ to maintain the number of times each character appears in a sliding window of length $l$, and use another hash table $freq$ to maintain the number of times each frequency appears. If $freq[k] = i$, that is, there are $i$ characters that appear $k$ times, then we have found a substring that meets the conditions. We can use two pointers to maintain this sliding window. Each time we move the right pointer, we increase the number of times the character pointed to by the right pointer appears and update the $freq$ array; each time we move the left pointer, we decrease the number of times the character pointed to by the left pointer appears and update the $freq$ array. After each pointer movement, we judge whether $freq[k]$ is equal to $i$. If it is equal, it means that we have found a substring that meets the conditions.
55+
56+
The time complexity is $O(n \times |\Sigma|)$, and the space complexity is $O(|\Sigma|)$, where $n$ is the length of the string $word$; and $\Sigma$ is the size of the character set. In this problem, the character set is lowercase English letters, so $|\Sigma| = 26$.
57+
4858
<!-- tabs:start -->
4959

5060
### **Python3**

solution/2900-2999/2954.Count the Number of Infection Sequences/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:组合数学 + 乘法逆元 + 快速幂**
64+
65+
根据题目描述,感冒的小朋友把还没有感冒的小朋友划分成了若干个连续段。我们可以用一个数组 $nums$ 记录每一段不感冒的小朋友的认识,不感冒人数一共有 $s = \sum_{i=0}^{k} nums[k]$ 人。我们可以发现,感冒序列的数目就是 $s$ 个不同元素的全排列数,即 $s!$。
66+
67+
假设每一段不感冒小朋友只有一种传播方案,那么一共有 $\frac{s!}{\prod_{i=0}^{k} nums[k]!}$ 种感冒序列。
68+
69+
接下来,我们再考虑每一段不感冒小朋友的传播方案。假设一段不感冒小朋友有 $x$ 个人,那么他们的传播方案有 $2^{x-1}$ 种,因为每一次可以从一段的左右两端选择其中一端传播,即:两种选择,一共有 $x-1$ 次传播。不过,如果是第一段或者最后一段,那么只有一种选择。
70+
71+
综上所述,总的感冒序列数目为:
72+
73+
$$
74+
\frac{s!}{\prod_{i=0}^{k} nums[k]!} \prod_{i=1}^{k-1} 2^{nums[i]-1}
75+
$$
76+
77+
最后,我们需要考虑到答案可能很大,需要对 $10^9 + 7$ 取模。因此,我们需要预处理阶乘和乘法逆元。
78+
79+
时间复杂度 $O(m)$,其中 $m$ 是数组 $sick$ 的长度。忽略预处理数组的空间消耗,空间复杂度 $O(m)$。
80+
6381
<!-- tabs:start -->
6482

6583
### **Python3**

solution/2900-2999/2954.Count the Number of Infection Sequences/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ Finally, the child at position 2 gets infected because it is adjacent to childre
5454

5555
## Solutions
5656

57+
**Solution 1: Combinatorial Mathematics + Multiplicative Inverse + Fast Power**
58+
59+
According to the problem description, the children who have a cold have divided the children who have not yet caught a cold into several continuous segments. We can use an array $nums$ to record the number of children who are not cold in each segment, and there are a total of $s = \sum_{i=0}^{k} nums[k]$ children who are not cold. We can find that the number of cold sequences is the number of permutations of $s$ different elements, that is, $s!$.
60+
61+
Assuming that there is only one transmission scheme for each segment of children who are not cold, there are $\frac{s!}{\prod_{i=0}^{k} nums[k]!}$ cold sequences in total.
62+
63+
Next, we consider the transmission scheme of each segment of children who are not cold. Suppose there are $x$ children in a segment who are not cold, then they have $2^{x-1}$ transmission schemes, because each time you can choose one end from the left and right ends of a segment to transmit, that is: two choices, there are a total of $x-1$ transmissions. However, if it is the first segment or the last segment, there is only one choice.
64+
65+
In summary, the total number of cold sequences is:
66+
67+
$$
68+
\frac{s!}{\prod_{i=0}^{k} nums[k]!} \prod_{i=1}^{k-1} 2^{nums[i]-1}
69+
$$
70+
71+
Finally, we need to consider that the answer may be very large and need to be modulo $10^9 + 7$. Therefore, we need to preprocess the factorial and multiplicative inverse.
72+
73+
The time complexity is $O(m)$, where $m$ is the length of the array $sick$. Ignoring the space consumption of the preprocessing array, the space complexity is $O(m)$.
74+
5775
<!-- tabs:start -->
5876

5977
### **Python3**

0 commit comments

Comments
 (0)