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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
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

Lines changed: 33 additions & 0 deletions
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
```
Lines changed: 20 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)