Skip to content

Commit 87761c2

Browse files
authored
feat: add solutions to lc problems: No.1692,1695,1732+ (doocs#2121)
1 parent 0e48286 commit 87761c2

File tree

16 files changed

+269
-5
lines changed

16 files changed

+269
-5
lines changed

solution/1600-1699/1692.Count Ways to Distribute Candies/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ func waysToDistribute(n int, k int) int {
155155
}
156156
```
157157

158+
### **TypeScript**
159+
160+
```ts
161+
function waysToDistribute(n: number, k: number): number {
162+
const mod = 10 ** 9 + 7;
163+
const f: number[][] = Array.from({ length: n + 1 }, () =>
164+
Array.from({ length: k + 1 }, () => 0),
165+
);
166+
f[0][0] = 1;
167+
for (let i = 1; i <= n; ++i) {
168+
for (let j = 1; j <= k; ++j) {
169+
f[i][j] = (f[i - 1][j] * j + f[i - 1][j - 1]) % mod;
170+
}
171+
}
172+
return f[n][k];
173+
}
174+
```
175+
158176
### **...**
159177

160178
```

solution/1600-1699/1692.Count Ways to Distribute Candies/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ func waysToDistribute(n int, k int) int {
145145
}
146146
```
147147

148+
### **TypeScript**
149+
150+
```ts
151+
function waysToDistribute(n: number, k: number): number {
152+
const mod = 10 ** 9 + 7;
153+
const f: number[][] = Array.from({ length: n + 1 }, () =>
154+
Array.from({ length: k + 1 }, () => 0),
155+
);
156+
f[0][0] = 1;
157+
for (let i = 1; i <= n; ++i) {
158+
for (let j = 1; j <= k; ++j) {
159+
f[i][j] = (f[i - 1][j] * j + f[i - 1][j - 1]) % mod;
160+
}
161+
}
162+
return f[n][k];
163+
}
164+
```
165+
148166
### **...**
149167

150168
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function waysToDistribute(n: number, k: number): number {
2+
const mod = 10 ** 9 + 7;
3+
const f: number[][] = Array.from({ length: n + 1 }, () =>
4+
Array.from({ length: k + 1 }, () => 0),
5+
);
6+
f[0][0] = 1;
7+
for (let i = 1; i <= n; ++i) {
8+
for (let j = 1; j <= k; ++j) {
9+
f[i][j] = (f[i - 1][j] * j + f[i - 1][j - 1]) % mod;
10+
}
11+
}
12+
return f[n][k];
13+
}

solution/1600-1699/1695.Maximum Erasure Value/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ func maximumUniqueSubarray(nums []int) (ans int) {
140140
}
141141
```
142142

143+
### **TypeScript**
144+
145+
```ts
146+
function maximumUniqueSubarray(nums: number[]): number {
147+
const m = Math.max(...nums);
148+
const n = nums.length;
149+
const s: number[] = Array.from({ length: n + 1 }, () => 0);
150+
for (let i = 1; i <= n; ++i) {
151+
s[i] = s[i - 1] + nums[i - 1];
152+
}
153+
const d = Array.from({ length: m + 1 }, () => 0);
154+
let [ans, j] = [0, 0];
155+
for (let i = 1; i <= n; ++i) {
156+
j = Math.max(j, d[nums[i - 1]]);
157+
ans = Math.max(ans, s[i] - s[j]);
158+
d[nums[i - 1]] = i;
159+
}
160+
return ans;
161+
}
162+
```
163+
143164
### **...**
144165

145166
```

solution/1600-1699/1695.Maximum Erasure Value/README_EN.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,27 @@ func maximumUniqueSubarray(nums []int) (ans int) {
130130
}
131131
```
132132

133+
### **TypeScript**
134+
135+
```ts
136+
function maximumUniqueSubarray(nums: number[]): number {
137+
const m = Math.max(...nums);
138+
const n = nums.length;
139+
const s: number[] = Array.from({ length: n + 1 }, () => 0);
140+
for (let i = 1; i <= n; ++i) {
141+
s[i] = s[i - 1] + nums[i - 1];
142+
}
143+
const d = Array.from({ length: m + 1 }, () => 0);
144+
let [ans, j] = [0, 0];
145+
for (let i = 1; i <= n; ++i) {
146+
j = Math.max(j, d[nums[i - 1]]);
147+
ans = Math.max(ans, s[i] - s[j]);
148+
d[nums[i - 1]] = i;
149+
}
150+
return ans;
151+
}
152+
```
153+
133154
### **...**
134155

135156
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maximumUniqueSubarray(nums: number[]): number {
2+
const m = Math.max(...nums);
3+
const n = nums.length;
4+
const s: number[] = Array.from({ length: n + 1 }, () => 0);
5+
for (let i = 1; i <= n; ++i) {
6+
s[i] = s[i - 1] + nums[i - 1];
7+
}
8+
const d = Array.from({ length: m + 1 }, () => 0);
9+
let [ans, j] = [0, 0];
10+
for (let i = 1; i <= n; ++i) {
11+
j = Math.max(j, d[nums[i - 1]]);
12+
ans = Math.max(ans, s[i] - s[j]);
13+
d[nums[i - 1]] = i;
14+
}
15+
return ans;
16+
}

solution/1700-1799/1732.Find the Highest Altitude/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@
3636

3737
## Solutions
3838

39+
**Solution 1: Prefix Sum (Difference Array)**
40+
41+
We assume the altitude of each point is $h_i$. Since $gain[i]$ represents the altitude difference between the $i$th point and the $(i + 1)$th point, we have $gain[i] = h_{i + 1} - h_i$. Therefore:
42+
43+
$$
44+
\sum_{i = 0}^{n-1} gain[i] = h_1 - h_0 + h_2 - h_1 + \cdots + h_n - h_{n - 1} = h_n - h_0 = h_n
45+
$$
46+
47+
which implies:
48+
49+
$$
50+
h_{i+1} = \sum_{j = 0}^{i} gain[j]
51+
$$
52+
53+
We can see that the altitude of each point can be calculated through the prefix sum. Therefore, we only need to traverse the array once, find the maximum value of the prefix sum, which is the highest altitude.
54+
55+
> In fact, the $gain$ array in the problem is a difference array. The prefix sum of the difference array gives the original altitude array. Then find the maximum value of the original altitude array.
56+
57+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array `gain`.
58+
3959
<!-- tabs:start -->
4060

4161
### **Python3**

solution/1700-1799/1734.Decode XORed Permutation/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737

3838
## Solutions
3939

40+
**Solution 1: Bitwise Operation**
41+
42+
We notice that the array $perm$ is a permutation of the first $n$ positive integers, so the XOR of all elements in $perm$ is $1 \oplus 2 \oplus \cdots \oplus n$, denoted as $a$. And $encode[i]=perm[i] \oplus perm[i+1]$, if we denote the XOR of all elements $encode[0],encode[2],\cdots,encode[n-3]$ as $b$, then $perm[n-1]=a \oplus b$. Knowing the last element of $perm$, we can find all elements of $perm$ by traversing the array $encode$ in reverse order.
43+
44+
The time complexity is $O(n)$, where $n$ is the length of the array $perm$. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
45+
4046
<!-- tabs:start -->
4147

4248
### **Python3**

solution/1700-1799/1735.Count Ways to Make Array With Product/README_EN.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@
3737

3838
## Solutions
3939

40+
**Solution 1: Prime Factorization + Combinatorial Mathematics**
41+
42+
We can perform prime factorization on $k$, i.e., $k = p_1^{x_1} \times p_2^{x_2} \times \cdots \times p_m^{x_m}$, where $p_i$ is a prime number, and $x_i$ is the exponent of $p_i$. The problem is equivalent to: placing $x_1$ $p_1$s, $x_2$ $p_2$s, $\cdots$, $x_m$ $p_m$s into $n$ positions respectively, where a single position can be empty. The question is how many schemes are there.
43+
44+
According to combinatorial mathematics, there are two cases when we put $x$ balls into $n$ boxes:
45+
46+
If the box cannot be empty, the number of schemes is $C_{x-1}^{n-1}$. This is using the partition method, i.e., there are a total of $x$ balls, and we insert $n-1$ partitions at $x-1$ positions, thereby dividing the $x$ balls into $n$ groups.
47+
48+
If the box can be empty, we can add $n$ virtual balls, and then use the partition method, i.e., there are a total of $x+n$ balls, and we insert $n-1$ partitions at $x+n-1$ positions, thereby dividing the actual $x$ balls into $n$ groups and allowing the box to be empty. Therefore, the number of schemes is $C_{x+n-1}^{n-1}$.
49+
50+
Therefore, for each query $queries[i]$, we can first perform prime factorization on $k$ to get the exponents $x_1, x_2, \cdots, x_m$, then calculate $C_{x_1+n-1}^{n-1}, C_{x_2+n-1}^{n-1}, \cdots, C_{x_m+n-1}^{n-1}$, and finally multiply all the scheme numbers.
51+
52+
So, the problem is transformed into how to quickly calculate $C_m^n$. According to the formula $C_m^n = \frac{m!}{n!(m-n)!}$, we can pre-process $m!$, and then use the inverse element to quickly calculate $C_m^n$.
53+
54+
The time complexity is $O(K \times \log \log K + N + m \times \log K)$, and the space complexity is $O(N)$.
55+
4056
<!-- tabs:start -->
4157

4258
### **Python3**

solution/1700-1799/1736.Latest Time by Replacing Hidden Digits/README_EN.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Greedy**
47+
48+
We process each digit of the string in order, following these rules:
49+
50+
1. First digit: If the value of the second digit is determined and falls within the range $[4, 9]$, then the first digit can only be $1$. Otherwise, the first digit can be up to $2$.
51+
1. Second digit: If the value of the first digit is determined and is $2$, then the second digit can be up to $3$. Otherwise, the second digit can be up to $9$.
52+
1. Third digit: The third digit can be up to $5$.
53+
1. Fourth digit: The fourth digit can be up to $9$.
54+
55+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
56+
4657
<!-- tabs:start -->
4758

4859
### **Python3**

0 commit comments

Comments
 (0)