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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)