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/2000-2099/2000.Reverse Prefix of Word/README_EN.md
+6
Original file line number
Diff line number
Diff line change
@@ -51,6 +51,12 @@ You should not do any reverse operation, the resulting string is "abcd"
51
51
52
52
## Solutions
53
53
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$.
Copy file name to clipboardexpand all lines: solution/2000-2099/2001.Number of Pairs of Interchangeable Rectangles/README_EN.md
+6
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,12 @@
45
45
46
46
## Solutions
47
47
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.
Copy file name to clipboardexpand all lines: solution/2000-2099/2002.Maximum Product of the Length of Two Palindromic Subsequences/README_EN.md
+8
Original file line number
Diff line number
Diff line change
@@ -48,6 +48,14 @@ The product of their lengths is: 5 * 5 = 25.
48
48
49
49
## Solutions
50
50
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$.
Copy file name to clipboardexpand all lines: solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README_EN.md
+16
Original file line number
Diff line number
Diff line change
@@ -56,6 +56,22 @@
56
56
57
57
## Solutions
58
58
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$.
Copy file name to clipboardexpand all lines: solution/2000-2099/2007.Find Original Array From Doubled Array/README_EN.md
+12
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,18 @@ Other original arrays could be [4,3,1] or [3,1,4].
47
47
48
48
## Solutions
49
49
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`.
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.
Copy file name to clipboardexpand all lines: solution/2000-2099/2011.Final Value of Variable After Performing Operations/README_EN.md
+6
Original file line number
Diff line number
Diff line change
@@ -63,6 +63,12 @@ X--: X is decremented by 1, X = 1 - 1 = 0.
63
63
64
64
## Solutions
65
65
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)$.
0 commit comments