You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/1700-1799/1732.Find the Highest Altitude/README_EN.md
+20
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,26 @@
36
36
37
37
## Solutions
38
38
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:
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`.
Copy file name to clipboardExpand all lines: solution/1700-1799/1734.Decode XORed Permutation/README_EN.md
+6
Original file line number
Diff line number
Diff line change
@@ -37,6 +37,12 @@
37
37
38
38
## Solutions
39
39
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)$.
Copy file name to clipboardExpand all lines: solution/1700-1799/1735.Count Ways to Make Array With Product/README_EN.md
+16
Original file line number
Diff line number
Diff line change
@@ -37,6 +37,22 @@
37
37
38
38
## Solutions
39
39
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)$.
Copy file name to clipboardExpand all lines: solution/1700-1799/1736.Latest Time by Replacing Hidden Digits/README_EN.md
+11
Original file line number
Diff line number
Diff line change
@@ -43,6 +43,17 @@
43
43
44
44
## Solutions
45
45
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)$.
Copy file name to clipboardExpand all lines: solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README_EN.md
+11-1
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,17 @@ The best way was done in 2 operations (either condition 1 or condition 3).
47
47
48
48
## Solutions
49
49
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$.
Copy file name to clipboardExpand all lines: solution/1700-1799/1738.Find Kth Largest XOR Coordinate Value/README_EN.md
+37-1
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,24 @@
47
47
48
48
## Solutions
49
49
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.,
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
+
50
68
<!-- tabs:start -->
51
69
52
70
### **Python3**
@@ -68,7 +86,6 @@ class Solution:
68
86
69
87
```java
70
88
classSolution {
71
-
72
89
publicintkthLargestValue(int[][] matrix, intk) {
73
90
int m = matrix.length, n = matrix[0].length;
74
91
int[][] s =newint[m +1][n +1];
@@ -127,6 +144,25 @@ func kthLargestValue(matrix [][]int, k int) int {
127
144
}
128
145
```
129
146
147
+
### **TypeScript**
148
+
149
+
```ts
150
+
function kthLargestValue(matrix:number[][], k:number):number {
Copy file name to clipboardExpand all lines: solution/1700-1799/1739.Building Boxes/README_EN.md
+10
Original file line number
Diff line number
Diff line change
@@ -55,6 +55,16 @@ These boxes are placed in the corner of the room, where the corner is on the bac
55
55
56
56
## Solutions
57
57
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)$.
0 commit comments