Skip to content

Commit 14238ec

Browse files
authoredDec 4, 2023
feat: add solutions to lc problems: No.2951~2954 (#2060)
1 parent efcefe3 commit 14238ec

File tree

12 files changed

+699
-7
lines changed

12 files changed

+699
-7
lines changed
 

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464

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

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

70-
综上所述,可以得到$newf0 = 3 \times f0 + 2 \times f1$, $newf1 = 2 \times f0 + 2 \times f1$。
70+
综上所述,可以得到 $newf0 = 3 \times f0 + 2 \times f1$, $newf1 = 2 \times f0 + 2 \times f1$。
7171

7272
时间复杂度 $O(n)$,其中 $n$ 是网格的行数。空间复杂度 $O(1)$。
7373

@@ -85,7 +85,7 @@ $$
8585

8686
最终的答案即为 $f[n][j]$ 的总和,其中 $j$ 是任意合法的状态。
8787

88-
我们注意到$f[i][j]$ 只和 $f[i - 1][k]$ 有关,因此我们可以使用滚动数组优化空间复杂度。
88+
我们注意到 $f[i][j]$ 只和 $f[i - 1][k]$ 有关,因此我们可以使用滚动数组优化空间复杂度。
8989

9090
时间复杂度 $O((m + n) \times 3^{2m})$,空间复杂度 $O(3^m)$。其中 $n$ 和 $m$ 分别是网格的行数和列数。
9191

‎solution/2900-2999/2951.Find the Peaks/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ mountain[2] 也不可能是峰值,因为它不严格大于 mountain[3] 和 mou
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:直接遍历**
57+
58+
我们直接遍历下标 $i \in [1, n-2]$,对于每个下标 $i$,如果满足 $mountain[i-1] < mountain[i]$ 并且 $mountain[i + 1] < mountain[i]$,那么 $mountain[i]$ 就是一个峰值,我们将下标 $i$ 加入答案数组中。
59+
60+
遍历结束后,返回答案数组即可。
61+
62+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**

‎solution/2900-2999/2951.Find the Peaks/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ So the answer is [1,3].
4747

4848
## Solutions
4949

50+
**Solution 1: Direct Traversal**
51+
52+
We directly traverse the index $i \in [1, n-2]$. For each index $i$, if $mountain[i-1] < mountain[i]$ and $mountain[i + 1] < mountain[i]$, then $mountain[i]$ is a peak, and we add index $i$ to the answer array.
53+
54+
After the traversal ends, we return the answer array.
55+
56+
The time complexity is $O(n)$, where $n$ is the length of the array. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
57+
5058
<!-- tabs:start -->
5159

5260
### **Python3**

‎solution/2900-2999/2952.Minimum Number of Coins to be Added/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:贪心 + 构造**
60+
61+
我们不妨假设当前需要构造的金额为 $s$,且我们已经构造出了 $[0,...,s-1]$ 内的所有金额。如果此时有一个新的硬币 $x$,我们把它加入到数组中,可以构造出 $[x, s+x-1]$ 内的所有金额。
62+
63+
接下来,分类讨论:
64+
65+
- 如果 $x \le s$,那么我们可以将上面两个区间合并,得到 $[0, s+x-1]$ 内的所有金额。
66+
- 如果 $x \gt s$,那么我们就需要添加一个面值为 $s$ 的硬币,这样可以构造出 $[0, 2s-1]$ 内的所有金额。然后我们再考虑 $x$ 和 $s$ 的大小关系,继续分析。
67+
68+
因此,我们将数组 $coins$ 按照升序排序,然后从小到大遍历数组中的硬币。对于每个硬币 $x$,我们进行上述分类讨论,直到 $s > target$ 为止。
69+
70+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组的长度。
71+
5972
<!-- tabs:start -->
6073

6174
### **Python3**

‎solution/2900-2999/2952.Minimum Number of Coins to be Added/README_EN.md

+13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ It can be shown that all integers from 1 to 20 are obtainable from the resulting
5151

5252
## Solutions
5353

54+
**Solution 1: Greedy + Construction**
55+
56+
Suppose the current amount we need to construct is $s$, and we have already constructed all amounts in $[0,...,s-1]$. If there is a new coin $x$, we add it to the array, which can construct all amounts in $[x, s+x-1]$.
57+
58+
Next, we discuss in two cases:
59+
60+
- If $x \le s$, then we can merge the two intervals above to get all amounts in $[0, s+x-1]$.
61+
- If $x \gt s$, then we need to add a coin with a face value of $s$, so that we can construct all amounts in $[0, 2s-1]$. Then we consider the size relationship between $x$ and $s$ and continue to analyze.
62+
63+
Therefore, we sort the array $coins$ in ascending order, and then traverse the coins in the array from small to large. For each coin $x$, we discuss in the above two cases until $s > target$.
64+
65+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array.
66+
5467
<!-- tabs:start -->
5568

5669
### **Python3**

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

+220-3
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,244 @@
6767
<!-- 这里可写当前语言的特殊实现逻辑 -->
6868

6969
```python
70-
70+
mod = 10**9 + 7
71+
mx = 10**5
72+
fac = [1] * (mx + 1)
73+
for i in range(2, mx + 1):
74+
fac[i] = fac[i - 1] * i % mod
75+
76+
77+
class Solution:
78+
def numberOfSequence(self, n: int, sick: List[int]) -> int:
79+
nums = [b - a - 1 for a, b in pairwise([-1] + sick + [n])]
80+
ans = 1
81+
s = sum(nums)
82+
ans = fac[s]
83+
for x in nums:
84+
if x:
85+
ans = ans * pow(fac[x], mod - 2, mod) % mod
86+
for x in nums[1:-1]:
87+
if x > 1:
88+
ans = ans * pow(2, x - 1, mod) % mod
89+
return ans
7190
```
7291

7392
### **Java**
7493

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

7796
```java
78-
97+
class Solution {
98+
private static final int MOD = (int) (1e9 + 7);
99+
private static final int MX = 100000;
100+
private static final int[] FAC = new int[MX + 1];
101+
102+
static {
103+
FAC[0] = 1;
104+
for (int i = 1; i <= MX; i++) {
105+
FAC[i] = (int) ((long) FAC[i - 1] * i % MOD);
106+
}
107+
}
108+
109+
public int numberOfSequence(int n, int[] sick) {
110+
int m = sick.length;
111+
int[] nums = new int[m + 1];
112+
nums[0] = sick[0];
113+
nums[m] = n - sick[m - 1] - 1;
114+
for (int i = 1; i < m; i++) {
115+
nums[i] = sick[i] - sick[i - 1] - 1;
116+
}
117+
int s = 0;
118+
for (int x : nums) {
119+
s += x;
120+
}
121+
int ans = FAC[s];
122+
for (int x : nums) {
123+
if (x > 0) {
124+
ans = (int) ((long) ans * qpow(FAC[x], MOD - 2) % MOD);
125+
}
126+
}
127+
for (int i = 1; i < nums.length - 1; ++i) {
128+
if (nums[i] > 1) {
129+
ans = (int) ((long) ans * qpow(2, nums[i] - 1) % MOD);
130+
}
131+
}
132+
return ans;
133+
}
134+
135+
private int qpow(long a, long n) {
136+
long ans = 1;
137+
for (; n > 0; n >>= 1) {
138+
if ((n & 1) == 1) {
139+
ans = ans * a % MOD;
140+
}
141+
a = a * a % MOD;
142+
}
143+
return (int) ans;
144+
}
145+
}
79146
```
80147

81148
### **C++**
82149

83150
```cpp
84-
151+
const int MX = 1e5;
152+
const int MOD = 1e9 + 7;
153+
int fac[MX + 1];
154+
155+
auto init = [] {
156+
fac[0] = 1;
157+
for (int i = 1; i <= MX; ++i) {
158+
fac[i] = 1LL * fac[i - 1] * i % MOD;
159+
}
160+
return 0;
161+
}();
162+
163+
int qpow(long long a, long long n) {
164+
long long ans = 1;
165+
for (; n > 0; n >>= 1) {
166+
if (n & 1) {
167+
ans = (ans * a) % MOD;
168+
}
169+
a = (a * a) % MOD;
170+
}
171+
return ans;
172+
}
173+
174+
class Solution {
175+
public:
176+
int numberOfSequence(int n, vector<int>& sick) {
177+
int m = sick.size();
178+
vector<int> nums(m + 1);
179+
180+
nums[0] = sick[0];
181+
nums[m] = n - sick[m - 1] - 1;
182+
for (int i = 1; i < m; i++) {
183+
nums[i] = sick[i] - sick[i - 1] - 1;
184+
}
185+
186+
int s = accumulate(nums.begin(), nums.end(), 0);
187+
long long ans = fac[s];
188+
for (int x : nums) {
189+
if (x > 0) {
190+
ans = ans * qpow(fac[x], MOD - 2) % MOD;
191+
}
192+
}
193+
for (int i = 1; i < nums.size() - 1; ++i) {
194+
if (nums[i] > 1) {
195+
ans = ans * qpow(2, nums[i] - 1) % MOD;
196+
}
197+
}
198+
return ans;
199+
}
200+
};
85201
```
86202
87203
### **Go**
88204
89205
```go
206+
const MX = 1e5
207+
const MOD = 1e9 + 7
208+
209+
var fac [MX + 1]int
210+
211+
func init() {
212+
fac[0] = 1
213+
for i := 1; i <= MX; i++ {
214+
fac[i] = fac[i-1] * i % MOD
215+
}
216+
}
217+
218+
func qpow(a, n int) int {
219+
ans := 1
220+
for n > 0 {
221+
if n&1 == 1 {
222+
ans = (ans * a) % MOD
223+
}
224+
a = (a * a) % MOD
225+
n >>= 1
226+
}
227+
return ans
228+
}
229+
230+
func numberOfSequence(n int, sick []int) int {
231+
m := len(sick)
232+
nums := make([]int, m+1)
233+
234+
nums[0] = sick[0]
235+
nums[m] = n - sick[m-1] - 1
236+
for i := 1; i < m; i++ {
237+
nums[i] = sick[i] - sick[i-1] - 1
238+
}
239+
240+
s := 0
241+
for _, x := range nums {
242+
s += x
243+
}
244+
ans := fac[s]
245+
for _, x := range nums {
246+
if x > 0 {
247+
ans = ans * qpow(fac[x], MOD-2) % MOD
248+
}
249+
}
250+
for i := 1; i < len(nums)-1; i++ {
251+
if nums[i] > 1 {
252+
ans = ans * qpow(2, nums[i]-1) % MOD
253+
}
254+
}
255+
return ans
256+
}
257+
```
90258

259+
### **TypeScript**
260+
261+
```ts
262+
const MX = 1e5;
263+
const MOD: bigint = BigInt(1e9 + 7);
264+
const fac: bigint[] = Array(MX + 1);
265+
266+
const init = (() => {
267+
fac[0] = 1n;
268+
for (let i = 1; i <= MX; ++i) {
269+
fac[i] = (fac[i - 1] * BigInt(i)) % MOD;
270+
}
271+
return 0;
272+
})();
273+
274+
function qpow(a: bigint, n: number): bigint {
275+
let ans = 1n;
276+
for (; n > 0; n >>= 1) {
277+
if (n & 1) {
278+
ans = (ans * a) % MOD;
279+
}
280+
a = (a * a) % MOD;
281+
}
282+
return ans;
283+
}
284+
285+
function numberOfSequence(n: number, sick: number[]): number {
286+
const m = sick.length;
287+
const nums: number[] = Array(m + 1);
288+
nums[0] = sick[0];
289+
nums[m] = n - sick[m - 1] - 1;
290+
for (let i = 1; i < m; i++) {
291+
nums[i] = sick[i] - sick[i - 1] - 1;
292+
}
293+
294+
const s = nums.reduce((acc, x) => acc + x, 0);
295+
let ans = fac[s];
296+
for (let x of nums) {
297+
if (x > 0) {
298+
ans = (ans * qpow(fac[x], Number(MOD) - 2)) % MOD;
299+
}
300+
}
301+
for (let i = 1; i < nums.length - 1; ++i) {
302+
if (nums[i] > 1) {
303+
ans = (ans * qpow(2n, nums[i] - 1)) % MOD;
304+
}
305+
}
306+
return Number(ans);
307+
}
91308
```
92309

93310
### **...**

0 commit comments

Comments
 (0)
Please sign in to comment.