Skip to content

Commit 2c6d560

Browse files
authored
feat: add solutions to lc problems: No.1790+ (doocs#2126)
1 parent f3b01f8 commit 2c6d560

File tree

52 files changed

+592
-22
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+592
-22
lines changed

solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@
5353

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

56-
**方法一:简单计数**
56+
**方法一:计数**
5757

5858
我们用变量 $cnt$ 记录两个字符串中相同位置字符不同的个数,两个字符串若满足题目要求,那么 $cnt$ 一定为 $0$ 或 $2$。另外用两个字符变量 $c1$ 和 $c2$ 记录两个字符串中相同位置字符不同的字符。
5959

6060
同时遍历两个字符串,对于相同位置的两个字符 $a$ 和 $b$,如果 $a \ne b$,那么 $cnt$ 自增 $1$。如果此时 $cnt$ 大于 $2$,或者 $cnt$ 为 $2$ 且 $a \ne c2$ 或 $b \ne c1$,那么直接返回 `false`。注意记录一下 $c1$ 和 $c2$。
6161

62-
遍历结束,若 $cnt\neq 1$,返回 `true`
62+
遍历结束,若 $cnt \neq 1$,返回 `true`
6363

64-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度
64+
时间复杂度 $O(n)$,其中 $n$ 为字符串长度。空间复杂度 $O(1)$。
6565

6666
<!-- tabs:start -->
6767

solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444

4545
## Solutions
4646

47+
**Solution 1: Counting**
48+
49+
We use a variable $cnt$ to record the number of characters at the same position in the two strings that are different. If the two strings meet the requirements of the problem, then $cnt$ must be $0$ or $2$. We also use two character variables $c1$ and $c2$ to record the characters that are different at the same position in the two strings.
50+
51+
While traversing the two strings simultaneously, for two characters $a$ and $b$ at the same position, if $a \ne b$, then $cnt$ is incremented by $1$. If at this time $cnt$ is greater than $2$, or $cnt$ is $2$ and $a \ne c2$ or $b \ne c1$, then we directly return `false`. Note to record $c1$ and $c2$.
52+
53+
At the end of the traversal, if $cnt \neq 1$, return `true`.
54+
55+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
56+
4757
<!-- tabs:start -->
4858

4959
### **Python3**

solution/1700-1799/1791.Find Center of Star Graph/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
中心点的特点是,它与其他所有点都相连,因此只要比较前两条边的点,如果有相同的点,那么这个点就是中心点。
5050

51-
时间复杂度 $O(1)$。
51+
时间复杂度 $O(1)$,空间复杂度 $O(1)$
5252

5353
<!-- tabs:start -->
5454

solution/1700-1799/1791.Find Center of Star Graph/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838

3939
## Solutions
4040

41+
**Solution 1: Directly Compare the Points of the First Two Edges**
42+
43+
The characteristic of the center point is that it is connected to all other points. Therefore, as long as we compare the points of the first two edges, if there are the same points, then this point is the center point.
44+
45+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
46+
4147
<!-- tabs:start -->
4248

4349
### **Python3**

solution/1700-1799/1792.Maximum Average Pass Ratio/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
最后,我们将所有班级的通过率求和,然后除以班级数目,即为答案。
5858

59-
时间复杂度 $O(n\log n)$,其中 $n$ 为班级数目。
59+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为班级数目。
6060

6161
<!-- tabs:start -->
6262

solution/1700-1799/1792.Maximum Average Pass Ratio/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040

4141
## Solutions
4242

43+
**Solution 1: Priority Queue (Max-Heap of Increment)**
44+
45+
Suppose a class currently has a pass rate of $\frac{a}{b}$. If we arrange a smart student into this class, then the pass rate of the class will become $\frac{a+1}{b+1}$. We can find that the increment of the pass rate is $\frac{a+1}{b+1} - \frac{a}{b}$.
46+
47+
We maintain a max-heap, which stores the increment of the pass rate for each class.
48+
49+
Perform `extraStudents` operations, each time taking a class from the top of the heap, adding $1$ to both the number of students and the number of passes in this class, then recalculating the increment of the pass rate of this class and putting it back into the heap. Repeat this process until all students are allocated.
50+
51+
Finally, we sum up the pass rates of all classes, and then divide by the number of classes to get the answer.
52+
53+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of classes.
54+
4355
<!-- tabs:start -->
4456

4557
### **Python3**

solution/1700-1799/1794.Count Pairs of Equal Substrings With Minimum Difference/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,31 @@ func countQuadruples(firstString string, secondString string) (ans int) {
160160
}
161161
```
162162

163+
### **TypeScript**
164+
165+
```ts
166+
function countQuadruples(firstString: string, secondString: string): number {
167+
const last: number[] = new Array(26).fill(0);
168+
for (let i = 0; i < secondString.length; ++i) {
169+
last[secondString.charCodeAt(i) - 97] = i + 1;
170+
}
171+
let [ans, mi] = [0, Infinity];
172+
for (let i = 0; i < firstString.length; ++i) {
173+
const j = last[firstString.charCodeAt(i) - 97];
174+
if (j) {
175+
const t = i - j;
176+
if (mi > t) {
177+
mi = t;
178+
ans = 1;
179+
} else if (mi === t) {
180+
++ans;
181+
}
182+
}
183+
}
184+
return ans;
185+
}
186+
```
187+
163188
### **...**
164189

165190
```

solution/1700-1799/1794.Count Pairs of Equal Substrings With Minimum Difference/README_EN.md

+33
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242

4343
## Solutions
4444

45+
**Solution 1: Greedy + Hash Table**
46+
47+
The problem actually asks us to find a smallest index $i$ and a largest index $j$ such that $firstString[i]$ equals $secondString[j]$, and the value of $i - j$ is the smallest among all index pairs that meet the conditions.
48+
49+
Therefore, we first use a hash table $last$ to record the index of the last occurrence of each character in $secondString$. Then we traverse $firstString$. For each character $c$, if $c$ has appeared in $secondString$, we calculate $i - last[c]$. If the value of $i - last[c]$ is less than the current minimum value, we update the minimum value and set the answer to 1. If the value of $i - last[c]$ equals the current minimum value, we increment the answer by 1.
50+
51+
The time complexity is $O(m + n)$, and the space complexity is $O(C)$. Here, $m$ and $n$ are the lengths of $firstString$ and $secondString$ respectively, and $C$ is the size of the character set. In this problem, $C = 26$.
52+
4553
<!-- tabs:start -->
4654

4755
### **Python3**
@@ -142,6 +150,31 @@ func countQuadruples(firstString string, secondString string) (ans int) {
142150
}
143151
```
144152

153+
### **TypeScript**
154+
155+
```ts
156+
function countQuadruples(firstString: string, secondString: string): number {
157+
const last: number[] = new Array(26).fill(0);
158+
for (let i = 0; i < secondString.length; ++i) {
159+
last[secondString.charCodeAt(i) - 97] = i + 1;
160+
}
161+
let [ans, mi] = [0, Infinity];
162+
for (let i = 0; i < firstString.length; ++i) {
163+
const j = last[firstString.charCodeAt(i) - 97];
164+
if (j) {
165+
const t = i - j;
166+
if (mi > t) {
167+
mi = t;
168+
ans = 1;
169+
} else if (mi === t) {
170+
++ans;
171+
}
172+
}
173+
}
174+
return ans;
175+
}
176+
```
177+
145178
### **...**
146179

147180
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function countQuadruples(firstString: string, secondString: string): number {
2+
const last: number[] = new Array(26).fill(0);
3+
for (let i = 0; i < secondString.length; ++i) {
4+
last[secondString.charCodeAt(i) - 97] = i + 1;
5+
}
6+
let [ans, mi] = [0, Infinity];
7+
for (let i = 0; i < firstString.length; ++i) {
8+
const j = last[firstString.charCodeAt(i) - 97];
9+
if (j) {
10+
const t = i - j;
11+
if (mi > t) {
12+
mi = t;
13+
ans = 1;
14+
} else if (mi === t) {
15+
++ans;
16+
}
17+
}
18+
}
19+
return ans;
20+
}

solution/1700-1799/1796.Second Largest Digit in a String/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
遍历结束,返回 $b$ 即可。
5151

52-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
52+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$
5353

5454
**方法二:位运算**
5555

@@ -59,7 +59,7 @@
5959

6060
最后,我们从高位向低位遍历 $mask$,找到第二个为 $1$ 的二进制位,其对应的数字即为第二大数字。如果不存在第二大数字,返回 $-1$。
6161

62-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
62+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$
6363

6464
<!-- tabs:start -->
6565

solution/1700-1799/1796.Second Largest Digit in a String/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@
3535

3636
## Solutions
3737

38+
**Solution 1: One Pass**
39+
40+
We define $a$ and $b$ to represent the largest and second largest numbers in the string, initially $a = b = -1$.
41+
42+
We traverse the string $s$. If the current character is a digit, we convert it to a number $v$. If $v > a$, it means that $v$ is the largest number currently appearing, we update $b$ to $a$, and update $a$ to $v$; if $v < a$, it means that $v$ is the second largest number currently appearing, we update $b$ to $v$.
43+
44+
After the traversal, we return $b$.
45+
46+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
47+
48+
**Solution 2: Bit Manipulation**
49+
50+
We can use an integer $mask$ to mark the numbers that appear in the string, where the $i$-th bit of $mask$ indicates whether the number $i$ has appeared.
51+
52+
We traverse the string $s$. If the current character is a digit, we convert it to a number $v$, and set the $v$-th bit of $mask$ to $1$.
53+
54+
Finally, we traverse $mask$ from high to low, find the second bit that is $1$, and the corresponding number is the second largest number. If there is no second largest number, return $-1$.
55+
56+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
57+
3858
<!-- tabs:start -->
3959

4060
### **Python3**

solution/1700-1799/1797.Design Authentication Manager/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ authenticationManager.<code>countUnexpiredTokens</code>(15); // The token with t
5454

5555
## Solutions
5656

57+
**Solution 1: Hash Table**
58+
59+
We can simply maintain a hash table $d$, where the key is `tokenId` and the value is the expiration time.
60+
61+
- During the `generate` operation, we store `tokenId` as the key and `currentTime + timeToLive` as the value in the hash table $d$.
62+
- During the `renew` operation, if `tokenId` is not in the hash table $d$, or `currentTime >= d[tokenId]`, we ignore this operation; otherwise, we update `d[tokenId]` to `currentTime + timeToLive`.
63+
- During the `countUnexpiredTokens` operation, we traverse the hash table $d$ and count the number of unexpired `tokenId`.
64+
65+
In terms of time complexity, both `generate` and `renew` operations have a time complexity of $O(1)$, and the `countUnexpiredTokens` operation has a time complexity of $O(n)$, where $n$ is the number of key-value pairs in the hash table $d$.
66+
67+
The space complexity is $O(n)$, where $n$ is the number of key-value pairs in the hash table $d$.
68+
5769
<!-- tabs:start -->
5870

5971
### **Python3**

solution/1700-1799/1798.Maximum Number of Consecutive Values You Can Make/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ func getMaximumConsecutive(coins []int) int {
140140
}
141141
```
142142

143+
### **TypeScript**
144+
145+
```ts
146+
function getMaximumConsecutive(coins: number[]): number {
147+
coins.sort((a, b) => a - b);
148+
let ans = 1;
149+
for (const v of coins) {
150+
if (v > ans) {
151+
break;
152+
}
153+
ans += v;
154+
}
155+
return ans;
156+
}
157+
```
158+
143159
### **...**
144160

145161
```

solution/1700-1799/1798.Maximum Number of Consecutive Values You Can Make/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ You can make 8 consecutive integer values starting from 0.</pre>
5454

5555
## Solutions
5656

57+
**Solution 1: Sorting + Greedy**
58+
59+
First, we sort the array. Then we define $ans$ as the current number of consecutive integers that can be constructed, initialized to $1$.
60+
61+
We traverse the array, for the current element $v$, if $v > ans$, it means that we cannot construct $ans+1$ consecutive integers, so we directly break the loop and return $ans$. Otherwise, it means that we can construct $ans+v$ consecutive integers, so we update $ans$ to $ans+v$.
62+
63+
Finally, we return $ans$.
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+
5767
<!-- tabs:start -->
5868

5969
### **Python3**
@@ -120,6 +130,22 @@ func getMaximumConsecutive(coins []int) int {
120130
}
121131
```
122132

133+
### **TypeScript**
134+
135+
```ts
136+
function getMaximumConsecutive(coins: number[]): number {
137+
coins.sort((a, b) => a - b);
138+
let ans = 1;
139+
for (const v of coins) {
140+
if (v > ans) {
141+
break;
142+
}
143+
ans += v;
144+
}
145+
return ans;
146+
}
147+
```
148+
123149
### **...**
124150

125151
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function getMaximumConsecutive(coins: number[]): number {
2+
coins.sort((a, b) => a - b);
3+
let ans = 1;
4+
for (const v of coins) {
5+
if (v > ans) {
6+
break;
7+
}
8+
ans += v;
9+
}
10+
return ans;
11+
}

solution/1700-1799/1799.Maximize Score After N Operations/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@
5757

5858
## Solutions
5959

60+
**Solution 1: State Compression + Dynamic Programming**
61+
62+
We can preprocess to get the greatest common divisor of any two numbers in the array `nums`, stored in the two-dimensional array $g$, where $g[i][j]$ represents the greatest common divisor of $nums[i]$ and $nums[j]$.
63+
64+
Then define $f[k]$ to represent the maximum score that can be obtained when the state after the current operation is $k$. Suppose $m$ is the number of elements in the array `nums`, then there are a total of $2^m$ states, that is, the range of $k$ is $[0, 2^m - 1]$.
65+
66+
Enumerate all states from small to large, for each state $k$, first determine whether the number of $1$s in the binary bits of this state $cnt$ is even, if so, perform the following operations:
67+
68+
Enumerate the positions where the binary bits in $k$ are 1, suppose they are $i$ and $j$, then the elements at positions $i$ and $j$ can perform one operation, and the score that can be obtained at this time is $\frac{cnt}{2} \times g[i][j]$, update the maximum value of $f[k]$.
69+
70+
The final answer is $f[2^m - 1]$.
71+
72+
The time complexity is $O(2^m \times m^2)$, and the space complexity is $O(2^m)$. Here, $m$ is the number of elements in the array `nums`.
73+
6074
<!-- tabs:start -->
6175

6276
### **Python3**

solution/1800-1899/1800.Maximum Ascending Subarray Sum/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@
6060

6161
**方法一:直接模拟**
6262

63-
我们用变量 `t` 记录当前升序子数组的和,用变量 `ans` 记录最大的升序子数组和。
63+
我们用变量 $t$ 记录当前升序子数组的和,用变量 $ans$ 记录最大的升序子数组和。
6464

65-
遍历数组 `nums`
65+
遍历数组 $nums$
6666

67-
如果当前元素是数组的第一个元素,或者当前元素大于前一个元素,那么将当前元素加入到当前升序子数组的和,即 `t += nums[i]`,并且更新最大升序子数组和 `ans = max(ans, t)`;否则,当前元素不满足升序子数组的条件,那么将当前升序子数组的和 `t` 重置为当前元素,即 `t = nums[i]`
67+
如果当前元素是数组的第一个元素,或者当前元素大于前一个元素,那么将当前元素加入到当前升序子数组的和,即 $t = t + nums[i]$,并且更新最大升序子数组和 $ans = \max(ans, t)$;否则,当前元素不满足升序子数组的条件,那么将当前升序子数组的和 $t$ 重置为当前元素,即 $t = nums[i]$
6868

69-
遍历结束,返回最大升序子数组和 `ans`
69+
遍历结束,返回最大升序子数组和 $ans$
7070

71-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
71+
时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$
7272

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

solution/1800-1899/1800.Maximum Ascending Subarray Sum/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@
4545

4646
## Solutions
4747

48+
**Solution 1: Direct Simulation**
49+
50+
We use a variable $t$ to record the current sum of the ascending subarray, and a variable $ans$ to record the maximum sum of the ascending subarray.
51+
52+
Traverse the array $nums$:
53+
54+
If the current element is the first element of the array, or the current element is greater than the previous one, then add the current element to the sum of the current ascending subarray, i.e., $t = t + nums[i]$, and update the maximum sum of the ascending subarray $ans = \max(ans, t)$. Otherwise, the current element does not satisfy the condition of the ascending subarray, so reset the sum $t$ of the current ascending subarray to the current element, i.e., $t = nums[i]$.
55+
56+
After the traversal, return the maximum sum of the ascending subarray $ans$.
57+
58+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
59+
4860
<!-- tabs:start -->
4961

5062
### **Python3**

0 commit comments

Comments
 (0)