Skip to content

Commit 8669e5b

Browse files
authored
feat: add solutions to lc problems: No.2000+ (doocs#2095)
1 parent 25c77f3 commit 8669e5b

File tree

41 files changed

+505
-238
lines changed

Some content is hidden

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

41 files changed

+505
-238
lines changed

solution/2000-2099/2000.Reverse Prefix of Word/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ You should not do any reverse operation, the resulting string is "abcd&quot
5151

5252
## Solutions
5353

54+
**Solution 1: Simulation**
55+
56+
First, we find the index $i$ where the character $ch$ first appears. Then, we reverse the characters from index $0$ to index $i$ (including index $i$). Finally, we concatenate the reversed string with the string starting from index $i + 1$.
57+
58+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $word$.
59+
5460
<!-- tabs:start -->
5561

5662
### **Python3**

solution/2000-2099/2001.Number of Pairs of Interchangeable Rectangles/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
为了能够唯一表示矩形,我们需要将矩形的宽高比化简为最简分数。因此,我们可以求出每个矩形的宽高比的最大公约数,然后将宽高比化简为最简分数。接下来,我们使用哈希表统计每个最简分数的矩形数量,然后计算每个最简分数的矩形数量的组合数,即可得到答案。
5757

58-
时间复杂度 $O(n \log M)$,空间复杂度 $O(n)$。其中 $n$ 和 $M$ 分别是矩形的数量和矩形的最大边长。
58+
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n)$。其中 $n$ 和 $M$ 分别是矩形的数量和矩形的最大边长。
5959

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

solution/2000-2099/2001.Number of Pairs of Interchangeable Rectangles/README_EN.md

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

4646
## Solutions
4747

48+
**Solution 1: Mathematics + Hash Table**
49+
50+
In order to uniquely represent a rectangle, we need to simplify the width-to-height ratio of the rectangle to a simplest fraction. Therefore, we can find the greatest common divisor of the width-to-height ratio of each rectangle, and then simplify the width-to-height ratio to the simplest fraction. Next, we use a hash table to count the number of rectangles for each simplest fraction, and then calculate the combination of the number of rectangles for each simplest fraction to get the answer.
51+
52+
The time complexity is $O(n \times \log M)$, and the space complexity is $O(n)$. Here, $n$ and $M$ are the number of rectangles and the maximum side length of the rectangles, respectively.
53+
4854
<!-- tabs:start -->
4955

5056
### **Python3**

solution/2000-2099/2002.Maximum Product of the Length of Two Palindromic Subsequences/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ The product of their lengths is: 5 * 5 = 25.
4848

4949
## Solutions
5050

51+
**Solution 1: Binary Enumeration**
52+
53+
We notice that the length of the string $s$ does not exceed $12$, so we can use the method of binary enumeration to enumerate all subsequences of $s$. Suppose the length of $s$ is $n$, we can use $2^n$ binary numbers of length $n$ to represent all subsequences of $s$. For each binary number, the $i$-th bit being $1$ means the $i$-th character of $s$ is in the subsequence, and $0$ means it is not in the subsequence. For each binary number, we judge whether it is a palindrome subsequence and record it in the array $p$.
54+
55+
Next, we enumerate each number $i$ in $p$. If $i$ is a palindrome subsequence, then we can enumerate a number $j$ from the complement of $i$, $mx = (2^n - 1) \oplus i$. If $j$ is also a palindrome subsequence, then $i$ and $j$ are the two palindrome subsequences we are looking for. Their lengths are the number of $1$s in the binary representation of $i$ and $j$, denoted as $a$ and $b$, respectively. Then their product is $a \times b$. We take the maximum of all possible $a \times b$.
56+
57+
The time complexity is $(2^n \times n + 3^n)$, and the space complexity is $O(2^n)$. Here, $n$ is the length of the string $s$.
58+
5159
<!-- tabs:start -->
5260

5361
### **Python3**

solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@
6161

6262
**方法一:暴力枚举**
6363

64-
我们注意到,数组 `nums` 的长度不超过 $200$,因此我们可以枚举所有的数对 $(i, j)$,其中 $i < j$,并判断 $|nums[i] - nums[j]|$ 是否等于 $k$,是则答案加一。
64+
我们注意到,数组 $nums$ 的长度不超过 $200$,因此我们可以枚举所有的数对 $(i, j)$,其中 $i < j$,并判断 $|nums[i] - nums[j]|$ 是否等于 $k$,是则答案加一。
6565

6666
最后返回答案即可。
6767

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

7070
**方法二:哈希表或数组**
7171

72-
我们可以使用哈希表或数组记录数组 `nums` 中每个数出现的次数,然后枚举数组 `nums` 中的每个数 $x$,判断 $x + k$ 和 $x - k$ 是否在数组 `nums` 中,是则答案加上 $x+k$ 和 $x-k$ 出现的次数之和。
72+
我们可以使用哈希表或数组记录数组 $nums$ 中每个数出现的次数,然后枚举数组 $nums$ 中的每个数 $x$,判断 $x + k$ 和 $x - k$ 是否在数组 $nums$ 中,是则答案加上 $x+k$ 和 $x-k$ 出现的次数之和。
7373

7474
最后返回答案即可。
7575

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

7878
<!-- tabs:start -->
7979

solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@
5656

5757
## Solutions
5858

59+
**Solution 1: Brute Force Enumeration**
60+
61+
We notice that the length of the array $nums$ does not exceed $200$, so we can enumerate all pairs $(i, j)$, where $i < j$, and check if $|nums[i] - nums[j]|$ equals $k$. If it does, we increment the answer by one.
62+
63+
Finally, we return the answer.
64+
65+
The time complexity is $O(n^2)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $nums$.
66+
67+
**Solution 2: Hash Table or Array**
68+
69+
We can use a hash table or array to record the occurrence count of each number in the array $nums$. Then, we enumerate each number $x$ in the array $nums$, and check if $x + k$ and $x - k$ are in the array $nums$. If they are, we increment the answer by the sum of the occurrence counts of $x + k$ and $x - k$.
70+
71+
Finally, we return the answer.
72+
73+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
74+
5975
<!-- tabs:start -->
6076

6177
### **Python3**

solution/2000-2099/2007.Find Original Array From Doubled Array/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ Other original arrays could be [4,3,1] or [3,1,4].
4747

4848
## Solutions
4949

50+
**Solution 1: Sorting + Counting + Traversal**
51+
52+
First, we check if the length $n$ of the array `changed` is odd. If it is, we directly return an empty array.
53+
54+
Then, we sort the array `changed`, and use a hash table or array `cnt` to count the occurrence of each element in `changed`.
55+
56+
Next, we traverse the array `changed`. For each element $x$ in `changed`, we first check if $x$ exists in the hash table `cnt`. If it does not exist, we directly skip this element. Otherwise, we check if $x \times 2$ exists in `cnt`. If it does not exist, we directly return an empty array. Otherwise, we add $x$ to the answer array `ans`, and decrease the occurrence counts of $x$ and $x \times 2$ in `cnt` by $1$ each.
57+
58+
After the traversal, we check if the length of the answer array `ans` is $\frac{n}{2}$. If it is, we return `ans`, otherwise we return an empty array.
59+
60+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array `changed`.
61+
5062
<!-- tabs:start -->
5163

5264
### **Python3**

solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ The resulting array is [1,2,3,4], which is continuous.
5757

5858
## Solutions
5959

60+
**Solution 1: Sorting + Deduplication + Binary Search**
61+
62+
First, we sort the array and remove duplicates.
63+
64+
Then, we traverse the array, enumerating the current element $nums[i]$ as the minimum value of the consecutive array. We use binary search to find the first position $j$ that is greater than $nums[i] + n - 1$. Then, $j-i$ is the length of the consecutive array when the current element is the minimum value. We update the answer, i.e., $ans = \min(ans, n - (j - i))$.
65+
66+
Finally, we return $ans$.
67+
68+
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.
69+
70+
**Solution 2: Sorting + Deduplication + Two Pointers**
71+
72+
Similar to Solution 1, we first sort the array and remove duplicates.
73+
74+
Then, we traverse the array, enumerating the current element $nums[i]$ as the minimum value of the consecutive array. We use two pointers to find the first position $j$ that is greater than $nums[i] + n - 1$. Then, $j-i$ is the length of the consecutive array when the current element is the minimum value. We update the answer, i.e., $ans = \min(ans, n - (j - i))$.
75+
76+
Finally, we return $ans$.
77+
78+
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.
79+
6080
<!-- tabs:start -->
6181

6282
### **Python3**

solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ X--:X 减 1 ,X = 1 - 1 = 0
7373

7474
遍历数组 `operations`,对于每个操作 $operations[i]$,如果包含 `'+'`,那么答案加 $1$,否则答案减 $1$。
7575

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

7878
<!-- tabs:start -->
7979

solution/2000-2099/2011.Final Value of Variable After Performing Operations/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ X--: X is decremented by 1, X = 1 - 1 = 0.
6363

6464
## Solutions
6565

66+
**Solution 1: Simulation**
67+
68+
Traverse the array `operations`. For each operation $operations[i]$, if it contains `'+'`, then the answer increases by $1$, otherwise the answer decreases by $1$.
69+
70+
The time complexity is $O(n)$, where $n$ is the length of the array `operations`. The space complexity is $O(1)$.
71+
6672
<!-- tabs:start -->
6773

6874
### **Python3**

solution/2000-2099/2012.Sum of Beauty in the Array/README.md

+68-61
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:预处理右侧最小值 + 遍历维护左侧最大值**
60+
61+
我们可以预处理出右侧最小值数组 $right$,其中 $right[i]$ 表示 $nums[i..n-1]$ 中的最小值。
62+
63+
然后我们从左到右遍历数组 $nums$,同时维护左侧最大值 $l$。对于每个位置 $i$,我们判断 $l < nums[i] < right[i + 1]$ 是否成立,如果成立则将 $2$ 累加至答案,否则判断 $nums[i - 1] < nums[i] < nums[i + 1]$ 是否成立,如果成立则将 $1$ 累加至答案。
64+
65+
遍历结束后即可得到答案。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
68+
5969
<!-- tabs:start -->
6070

6171
### **Python3**
@@ -66,18 +76,18 @@
6676
class Solution:
6777
def sumOfBeauties(self, nums: List[int]) -> int:
6878
n = len(nums)
69-
lmx = [0] * n
70-
for i in range(1, n):
71-
lmx[i] = max(lmx[i - 1], nums[i - 1])
72-
rmi = [100001] * n
79+
right = [nums[-1]] * n
7380
for i in range(n - 2, -1, -1):
74-
rmi[i] = min(rmi[i + 1], nums[i + 1])
81+
right[i] = min(right[i + 1], nums[i])
7582
ans = 0
83+
l = nums[0]
7684
for i in range(1, n - 1):
77-
if lmx[i] < nums[i] < rmi[i]:
85+
r = right[i + 1]
86+
if l < nums[i] < r:
7887
ans += 2
7988
elif nums[i - 1] < nums[i] < nums[i + 1]:
8089
ans += 1
90+
l = max(l, nums[i])
8191
return ans
8292
```
8393

@@ -89,70 +99,47 @@ class Solution:
8999
class Solution {
90100
public int sumOfBeauties(int[] nums) {
91101
int n = nums.length;
92-
int[] lmx = new int[n];
93-
int[] rmi = new int[n];
94-
rmi[n - 1] = 100001;
95-
for (int i = 1; i < n; ++i) {
96-
lmx[i] = Math.max(lmx[i - 1], nums[i - 1]);
97-
}
98-
for (int i = n - 2; i >= 0; --i) {
99-
rmi[i] = Math.min(rmi[i + 1], nums[i + 1]);
102+
int[] right = new int[n];
103+
right[n - 1] = nums[n - 1];
104+
for (int i = n - 2; i > 0; --i) {
105+
right[i] = Math.min(right[i + 1], nums[i]);
100106
}
101107
int ans = 0;
108+
int l = nums[0];
102109
for (int i = 1; i < n - 1; ++i) {
103-
if (lmx[i] < nums[i] && nums[i] < rmi[i]) {
110+
int r = right[i + 1];
111+
if (l < nums[i] && nums[i] < r) {
104112
ans += 2;
105113
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
106114
ans += 1;
107115
}
116+
l = Math.max(l, nums[i]);
108117
}
109118
return ans;
110119
}
111120
}
112121
```
113122

114-
### \*\*TypeScript
115-
116-
```ts
117-
function sumOfBeauties(nums: number[]): number {
118-
let n = nums.length;
119-
let prefix = new Array(n),
120-
postfix = new Array(n);
121-
prefix[0] = nums[0];
122-
postfix[n - 1] = nums[n - 1];
123-
for (let i = 1, j = n - 2; i < n; ++i, --j) {
124-
prefix[i] = Math.max(nums[i], prefix[i - 1]);
125-
postfix[j] = Math.min(nums[j], postfix[j + 1]);
126-
}
127-
let ans = 0;
128-
for (let i = 1; i < n - 1; ++i) {
129-
if (prefix[i - 1] < nums[i] && nums[i] < postfix[i + 1]) {
130-
ans += 2;
131-
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
132-
ans += 1;
133-
}
134-
}
135-
return ans;
136-
}
137-
```
138-
139123
### **C++**
140124

141125
```cpp
142126
class Solution {
143127
public:
144128
int sumOfBeauties(vector<int>& nums) {
145129
int n = nums.size();
146-
vector<int> lmx(n);
147-
vector<int> rmi(n, 100001);
148-
for (int i = 1; i < n; ++i) lmx[i] = max(lmx[i - 1], nums[i - 1]);
149-
for (int i = n - 2; i >= 0; --i) rmi[i] = min(rmi[i + 1], nums[i + 1]);
130+
vector<int> right(n, nums[n - 1]);
131+
for (int i = n - 2; i; --i) {
132+
right[i] = min(right[i + 1], nums[i]);
133+
}
150134
int ans = 0;
151-
for (int i = 1; i < n - 1; ++i) {
152-
if (lmx[i] < nums[i] && nums[i] < rmi[i])
135+
for (int i = 1, l = nums[0]; i < n - 1; ++i) {
136+
int r = right[i + 1];
137+
if (l < nums[i] && nums[i] < r) {
153138
ans += 2;
154-
else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1])
139+
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
155140
ans += 1;
141+
}
142+
l = max(l, nums[i]);
156143
}
157144
return ans;
158145
}
@@ -162,26 +149,46 @@ public:
162149
### **Go**
163150
164151
```go
165-
func sumOfBeauties(nums []int) int {
152+
func sumOfBeauties(nums []int) (ans int) {
166153
n := len(nums)
167-
lmx := make([]int, n)
168-
rmi := make([]int, n)
169-
rmi[n-1] = 100001
170-
for i := 1; i < n; i++ {
171-
lmx[i] = max(lmx[i-1], nums[i-1])
172-
}
173-
for i := n - 2; i >= 0; i-- {
174-
rmi[i] = min(rmi[i+1], nums[i+1])
154+
right := make([]int, n)
155+
right[n-1] = nums[n-1]
156+
for i := n - 2; i > 0; i-- {
157+
right[i] = min(right[i+1], nums[i])
175158
}
176-
ans := 0
177-
for i := 1; i < n-1; i++ {
178-
if lmx[i] < nums[i] && nums[i] < rmi[i] {
159+
for i, l := 1, nums[0]; i < n-1; i++ {
160+
r := right[i+1]
161+
if l < nums[i] && nums[i] < r {
179162
ans += 2
180163
} else if nums[i-1] < nums[i] && nums[i] < nums[i+1] {
181-
ans += 1
164+
ans++
182165
}
166+
l = max(l, nums[i])
183167
}
184-
return ans
168+
return
169+
}
170+
```
171+
172+
### **TypeScript**
173+
174+
```ts
175+
function sumOfBeauties(nums: number[]): number {
176+
const n = nums.length;
177+
const right: number[] = Array(n).fill(nums[n - 1]);
178+
for (let i = n - 2; i; --i) {
179+
right[i] = Math.min(right[i + 1], nums[i]);
180+
}
181+
let ans = 0;
182+
for (let i = 1, l = nums[0]; i < n - 1; ++i) {
183+
const r = right[i + 1];
184+
if (l < nums[i] && nums[i] < r) {
185+
ans += 2;
186+
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
187+
ans += 1;
188+
}
189+
l = Math.max(l, nums[i]);
190+
}
191+
return ans;
185192
}
186193
```
187194

0 commit comments

Comments
 (0)