Skip to content

Commit 87761c2

Browse files
authored
feat: add solutions to lc problems: No.1692,1695,1732+ (#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

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

+18
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
```
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

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

+21
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
```
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

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

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

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

+11
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**

solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README_EN.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,17 @@ The best way was done in 2 operations (either condition 1 or condition 3).
4747

4848
## Solutions
4949

50-
Prefix Sum
50+
**Solution 1: Counting + Enumeration**
51+
52+
First, we count the number of occurrences of each letter in strings $a$ and $b$, denoted as $cnt_1$ and $cnt_2$.
53+
54+
Then, we consider condition $3$, i.e., every letter in $a$ and $b$ is the same. We just need to enumerate the final letter $c$, and then count the number of letters in $a$ and $b$ that are not $c$. This is the number of characters that need to be changed.
55+
56+
Next, we consider conditions $1$ and $2$, i.e., every letter in $a$ is less than every letter in $b$, or every letter in $b$ is less than every letter in $a$. For condition $1$, we make all characters in string $a$ less than character $c$, and all characters in string $b$ not less than $c$. We enumerate $c$ to find the smallest answer. Condition $2$ is similar.
57+
58+
The final answer is the minimum of the above three cases.
59+
60+
The time complexity is $O(m + n + C^2)$, where $m$ and $n$ are the lengths of strings $a$ and $b$ respectively, and $C$ is the size of the character set. In this problem, $C = 26$.
5161

5262
<!-- tabs:start -->
5363

solution/1700-1799/1738.Find Kth Largest XOR Coordinate Value/README.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,23 @@
5454

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

57-
二维前缀异或,然后求第 k 大的值即可。
57+
**方法一:二维前缀异或 + 排序或快速选择**
58+
59+
我们定义一个二维前缀异或数组 $s$,其中 $s[i][j]$ 表示矩阵前 $i$ 行和前 $j$ 列的元素异或运算的结果,即:
60+
61+
$$
62+
s[i][j] = \bigoplus_{0 \leq x \leq i, 0 \leq y \leq j} matrix[x][y]
63+
$$
64+
65+
而 $s[i][j]$ 可以由 $s[i - 1][j]$, $s[i][j - 1]$ 和 $s[i - 1][j - 1]$ 三个元素计算得到,即:
66+
67+
$$
68+
s[i][j] = s[i - 1][j] \oplus s[i][j - 1] \oplus s[i - 1][j - 1] \oplus matrix[i - 1][j - 1]
69+
$$
70+
71+
我们遍历矩阵,计算出所有的 $s[i][j]$,然后将其排序,最后返回第 $k$ 大的元素即可。如果不想使用排序,也可以使用快速选择算法,这样可以优化时间复杂度。
72+
73+
时间复杂度 $O(m \times n \times \log (m \times n))$ 或 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
5874

5975
<!-- tabs:start -->
6076

@@ -81,7 +97,6 @@ class Solution:
8197

8298
```java
8399
class Solution {
84-
85100
public int kthLargestValue(int[][] matrix, int k) {
86101
int m = matrix.length, n = matrix[0].length;
87102
int[][] s = new int[m + 1][n + 1];
@@ -140,6 +155,25 @@ func kthLargestValue(matrix [][]int, k int) int {
140155
}
141156
```
142157

158+
### **TypeScript**
159+
160+
```ts
161+
function kthLargestValue(matrix: number[][], k: number): number {
162+
const m: number = matrix.length;
163+
const n: number = matrix[0].length;
164+
const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => 0));
165+
const ans: number[] = [];
166+
for (let i = 0; i < m; ++i) {
167+
for (let j = 0; j < n; ++j) {
168+
s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j];
169+
ans.push(s[i + 1][j + 1]);
170+
}
171+
}
172+
ans.sort((a, b) => b - a);
173+
return ans[k - 1];
174+
}
175+
```
176+
143177
### **...**
144178

145179
```

solution/1700-1799/1738.Find Kth Largest XOR Coordinate Value/README_EN.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@
4747

4848
## Solutions
4949

50+
**Solution 1: Two-dimensional Prefix XOR + Sorting or Quick Selection**
51+
52+
We define a two-dimensional prefix XOR array $s$, where $s[i][j]$ represents the XOR result of the elements in the first $i$ rows and the first $j$ columns of the matrix, i.e.,
53+
54+
$$
55+
s[i][j] = \bigoplus_{0 \leq x \leq i, 0 \leq y \leq j} matrix[x][y]
56+
$$
57+
58+
And $s[i][j]$ can be calculated from the three elements $s[i - 1][j]$, $s[i][j - 1]$ and $s[i - 1][j - 1]$, i.e.,
59+
60+
$$
61+
s[i][j] = s[i - 1][j] \oplus s[i][j - 1] \oplus s[i - 1][j - 1] \oplus matrix[i - 1][j - 1]
62+
$$
63+
64+
We traverse the matrix, calculate all $s[i][j]$, then sort them, and finally return the $k$th largest element. If you don't want to use sorting, you can also use the quick selection algorithm, which can optimize the time complexity.
65+
66+
The time complexity is $O(m \times n \times \log (m \times n))$ or $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively.
67+
5068
<!-- tabs:start -->
5169

5270
### **Python3**
@@ -68,7 +86,6 @@ class Solution:
6886

6987
```java
7088
class Solution {
71-
7289
public int kthLargestValue(int[][] matrix, int k) {
7390
int m = matrix.length, n = matrix[0].length;
7491
int[][] s = new int[m + 1][n + 1];
@@ -127,6 +144,25 @@ func kthLargestValue(matrix [][]int, k int) int {
127144
}
128145
```
129146

147+
### **TypeScript**
148+
149+
```ts
150+
function kthLargestValue(matrix: number[][], k: number): number {
151+
const m: number = matrix.length;
152+
const n: number = matrix[0].length;
153+
const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => 0));
154+
const ans: number[] = [];
155+
for (let i = 0; i < m; ++i) {
156+
for (let j = 0; j < n; ++j) {
157+
s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j];
158+
ans.push(s[i + 1][j + 1]);
159+
}
160+
}
161+
ans.sort((a, b) => b - a);
162+
return ans[k - 1];
163+
}
164+
```
165+
130166
### **...**
131167

132168
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function kthLargestValue(matrix: number[][], k: number): number {
2+
const m: number = matrix.length;
3+
const n: number = matrix[0].length;
4+
const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => 0));
5+
const ans: number[] = [];
6+
for (let i = 0; i < m; ++i) {
7+
for (let j = 0; j < n; ++j) {
8+
s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j];
9+
ans.push(s[i + 1][j + 1]);
10+
}
11+
}
12+
ans.sort((a, b) => b - a);
13+
return ans[k - 1];
14+
}

solution/1700-1799/1739.Building Boxes/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
如果此时盒子还有剩余,那么可以从最低一层继续摆放,假设摆放 $i$ 个,那么累计可摆放的盒子个数为 $1+2+\cdots+i$。
7171

72-
时间复杂度 $O(\sqrt{n})$,空间复杂度 $O(1)$。其中 $n$ 为题目给定的盒子数量
72+
时间复杂度 $O(\sqrt{n})$,其中 $n$ 为题目给定的盒子数量。空间复杂度 $O(1)$。
7373

7474
<!-- tabs:start -->
7575

solution/1700-1799/1739.Building Boxes/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ These boxes are placed in the corner of the room, where the corner is on the bac
5555

5656
## Solutions
5757

58+
**Solution 1: Mathematical Rule**
59+
60+
According to the problem description, the box with the highest number of layers needs to be placed in the corner of the wall, and the arrangement of the boxes is in a step-like shape, which can minimize the number of boxes touching the ground.
61+
62+
Assume that the boxes are arranged in $k$ layers. From top to bottom, if each layer is filled, then the number of boxes in each layer is $1, 1+2, 1+2+3, \cdots, 1+2+\cdots+k$.
63+
64+
If there are still remaining boxes at this point, they can continue to be placed from the lowest layer. Assume that $i$ boxes are placed, then the cumulative number of boxes that can be placed is $1+2+\cdots+i$.
65+
66+
The time complexity is $O(\sqrt{n})$, where $n$ is the number of boxes given in the problem. The space complexity is $O(1)$.
67+
5868
<!-- tabs:start -->
5969

6070
### **Python3**

0 commit comments

Comments
 (0)