Skip to content

Commit 0e48286

Browse files
authored
feat: add solutions to lc problems: No.1600+ (doocs#2119)
1 parent 3d74b5a commit 0e48286

File tree

41 files changed

+478
-27
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

+478
-27
lines changed

solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ nums[3] + nums[0] = 3 + 1 = 4.
8080

8181
可以发现,这实际上是在对一个连续区间内的元素进行加减操作,因此我们可以使用差分数组来实现。
8282

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

8585
<!-- tabs:start -->
8686

solution/1600-1699/1674.Minimum Moves to Make Array Complementary/README_EN.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.
5252

5353
## Solutions
5454

55+
**Solution 1: Difference Array**
56+
57+
Let's denote $a$ as the smaller value between $nums[i]$ and $nums[n-i-1]$, and $b$ as the larger value between $nums[i]$ and $nums[n-i-1]$.
58+
59+
Suppose that after replacement, the sum of the two numbers is $x$. From the problem, we know that the minimum value of $x$ is $2$, which means both numbers are replaced by $1$; the maximum value is $2 \times limit$, which means both numbers are replaced by $limit$. Therefore, the range of $x$ is $[2,... 2 \times limit]$.
60+
61+
How to find the minimum number of replacements for different $x$?
62+
63+
We analyze and find:
64+
65+
- If $x = a + b$, then the number of replacements we need is $0$, which means the current pair of numbers already meets the complement requirement;
66+
- Otherwise, if $1 + a \le x \le limit + b $, then the number of replacements we need is $1$, which means we can replace one of the numbers;
67+
- Otherwise, if $2 \le x \le 2 \times limit$, then the number of replacements we need is $2$, which means we need to replace both numbers.
68+
69+
Therefore, we can iterate over each pair of numbers and perform the following operations:
70+
71+
1. First, add $2$ to the number of operations required in the range $[2,... 2 \times limit]$.
72+
1. Then, subtract $1$ from the number of operations required in the range $[1 + a,... limit + b]$.
73+
1. Finally, subtract $1$ from the number of operations required in the range $[a + b,... a + b]$.
74+
75+
We can see that this is actually adding and subtracting elements in a continuous interval, so we can use a difference array to implement it.
76+
77+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
78+
5579
<!-- tabs:start -->
5680

5781
### **Python3**

solution/1600-1699/1675.Minimize Deviation in Array/README_EN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@
6060

6161
## Solutions
6262

63+
**Solution 1: Greedy + Priority Queue**
64+
65+
Intuitively, to get the minimum offset of the array, we need to decrease the maximum value of the array and increase the minimum value of the array.
66+
67+
Since there are two operations that can be performed each time: multiply an odd number by $2$; divide an even number by $2$, the situation is more complex. We can multiply all odd numbers by $2$ to convert them into even numbers, which is equivalent to having only one division operation. The division operation can only reduce a certain number, and only by reducing the maximum value can the result be more optimal.
68+
69+
Therefore, we use a priority queue (max heap) to maintain the maximum value of the array. Each time we take out the top element of the heap for division operation, put the new value into the heap, and update the minimum value and the minimum value of the difference between the top element of the heap and the minimum value.
70+
71+
When the top element of the heap is an odd number, the operation stops.
72+
73+
The time complexity is $O(n\log n \times \log m)$. Where $n$ and $m$ are the length of the array `nums` and the maximum element of the array, respectively. Since the maximum element in the array is divided by $2$ at most $O(\log m)$ times, all elements are divided by $2$ at most $O(n\log m)$ times. Each time the heap is popped and put into operation, the time complexity is $O(\log n)$. Therefore, the total time complexity is $O(n\log n \times \log m)$.
74+
6375
<!-- tabs:start -->
6476

6577
### **Python3**

solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ The final concatenated result is &quot;Goal&quot;.
4545

4646
## Solutions
4747

48+
**Solution 1: String Replacement**
49+
50+
According to the problem, we only need to replace `"()"` with `'o'` and `"(al)"` with `"al"` in the string `command`.
51+
52+
**Solution 2: String Iteration**
53+
54+
We can also iterate over the string `command`. For each character $c$:
55+
56+
- If it is `'G'`, directly add $c$ to the result string;
57+
- If it is `'('`, check if the next character is `')'`. If it is, add `'o'` to the result string. Otherwise, add `"al"` to the result string.
58+
59+
After the iteration, return the result string.
60+
61+
The time complexity is $O(n)$, and the space complexity is $O(1)$.
62+
4863
<!-- tabs:start -->
4964

5065
### **Python3**

solution/1600-1699/1679.Max Number of K-Sum Pairs/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
**方法一:排序**
5151

52-
我们对 `nums` 进行排序。然后 $l$, $r$ 分别指向 `nums` 首尾元素,判断两整数之和 $s$ 与 $k$ 的大小关系。
52+
我们对 $nums$ 进行排序。然后 $l$, $r$ 分别指向 $nums$ 首尾元素,判断两整数之和 $s$ 与 $k$ 的大小关系。
5353

5454
- 若 $s = k$,说明找到了两个整数,满足和为 $k$,答案加一,然后 $l$, $r$ 向中间移动;
5555
- 若 $s \gt k$,则 $r$ 指针向左移动;
@@ -58,17 +58,17 @@
5858

5959
循环结束,返回答案。
6060

61-
时间复杂度 $O(n\times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为 `nums` 的长度。
61+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为 $nums$ 的长度。
6262

6363
**方法二:哈希表**
6464

65-
我们使用哈希表 `cnt` 记录当前剩余整数及其出现的次数。
65+
我们使用哈希表 $cnt$ 记录当前剩余整数及其出现的次数。
6666

67-
遍历 `nums`,对于当前整数 $x$,判断 $k - x$ 是否在 `cnt` 中,若存在,则说明找到了两个整数,满足和为 $k$,答案加一,然后将 $k - x$ 的出现次数减一;否则,将 $x$ 的出现次数加一。
67+
遍历 $nums$,对于当前整数 $x$,判断 $k - x$ 是否在 $cnt$ 中,若存在,则说明找到了两个整数,满足和为 $k$,答案加一,然后将 $k - x$ 的出现次数减一;否则,将 $x$ 的出现次数加一。
6868

6969
遍历结束,返回答案。
7070

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

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

solution/1600-1699/1679.Max Number of K-Sum Pairs/README_EN.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ There are no more pairs that sum up to 6, hence a total of 1 operation.</pre>
4141

4242
## Solutions
4343

44+
**Solution 1: Sorting**
45+
46+
We sort $nums$. Then $l$ and $r$ point to the first and last elements of $nums$ respectively, and we compare the sum $s$ of the two integers with $k$.
47+
48+
- If $s = k$, it means that we have found two integers whose sum is $k$. We increment the answer and then move $l$ and $r$ towards the middle;
49+
- If $s > k$, then we move the $r$ pointer to the left;
50+
- If $s < k$, then we move the $l$ pointer to the right;
51+
- We continue the loop until $l \geq r$.
52+
53+
After the loop ends, we return the answer.
54+
55+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of $nums$.
56+
57+
**Solution 2: Hash Table**
58+
59+
We use a hash table $cnt$ to record the current remaining integers and their occurrence counts.
60+
61+
We iterate over $nums$. For the current integer $x$, we check if $k - x$ is in $cnt$. If it exists, it means that we have found two integers whose sum is $k$. We increment the answer and then decrement the occurrence count of $k - x$; otherwise, we increment the occurrence count of $x$.
62+
63+
After the iteration ends, we return the answer.
64+
65+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of $nums$.
66+
4467
<!-- tabs:start -->
4568

4669
### **Python3**

solution/1600-1699/1680.Concatenation of Consecutive Binary Numbers/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
观察数字的连接规律,我们可以发现,当连接到第 $i$ 个数时,实际上是将前 $i-1$ 个数连接而成的结果 $ans$ 往左移动一定的位数,然后再加上 $i$ 这个数,移动的位数 $shift$ 是 $i$ 中二进制的位数。由于 $i$ 在不断加 $1$,移动的位数要么与上一次移动的位数保持不变,要么加一。当 $i$ 为 $2$ 的幂次方的时候,也即是说 $i$ 的二进制数中只有一位是 $1$ 时,移动的位数相比于上次加 $1$。
5252

53-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为给定的整数
53+
时间复杂度 $O(n)$,其中 $n$ 为给定的整数。空间复杂度 $O(1)$。
5454

5555
<!-- tabs:start -->
5656

solution/1600-1699/1680.Concatenation of Consecutive Binary Numbers/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ After modulo 10<sup>9</sup> + 7, the result is 505379714.
4343

4444
## Solutions
4545

46+
**Solution 1: Bit Manipulation**
47+
48+
By observing the pattern of number concatenation, we can find that when concatenating to the $i$-th number, the result $ans$ formed by concatenating the previous $i-1$ numbers is actually shifted to the left by a certain number of bits, and then $i$ is added. The number of bits shifted, $shift$, is the number of binary digits in $i$. Since $i$ is continuously incremented by $1$, the number of bits shifted either remains the same as the last shift or increases by one. When $i$ is a power of $2$, that is, when there is only one bit in the binary number of $i$ that is $1$, the number of bits shifted increases by $1$ compared to the last time.
49+
50+
The time complexity is $O(n)$, where $n$ is the given integer. The space complexity is $O(1)$.
51+
4652
<!-- tabs:start -->
4753

4854
### **Python3**

solution/1600-1699/1681.Minimum Incompatibility/README_EN.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ The incompatibility is (2-1) + (3-2) + (8-6) + (3-1) = 6.
5050

5151
## Solutions
5252

53+
**Solution 1: Preprocessing + State Compression + Dynamic Programming**
54+
55+
Let's assume that the size of each subset after partitioning is $m$, so $m=\frac{n}{k}$, where $n$ is the length of the array.
56+
57+
We can enumerate all subsets $i$, where $i \in [0, 2^n)$, if the binary representation of subset $i$ has $m$ ones, and the elements in subset $i$ are not repeated, then we can calculate the incompatibility of subset $i$, denoted as $g[i]$, i.e., $g[i]=\max_{j \in i} \{nums[j]\} - \min_{j \in i} \{nums[j]\}$.
58+
59+
Next, we can use dynamic programming to solve.
60+
61+
We define $f[i]$ as the minimum sum of incompatibilities when the current partitioned subset state is $i$. Initially, $f[0]=0$, which means no elements are partitioned into the subset, and the rest $f[i]=+\infty$.
62+
63+
For state $i$, we find all undivided and non-repeated elements, represented by a state $mask$. If the number of elements in state $mask$ is greater than or equal to $m$, then we enumerate all subsets $j$ of $mask$, and satisfy $j \subset mask$, then $f[i \cup j]=\min \{f[i \cup j], f[i]+g[j]\}$.
64+
65+
Finally, if $f[2^n-1]=+\infty$, it means that it cannot be partitioned into $k$ subsets, return $-1$, otherwise return $f[2^n-1]$.
66+
67+
The time complexity is $O(3^n)$, and the space complexity is $O(2^n)$. Here, $n$ is the length of the array.
68+
5369
<!-- tabs:start -->
5470

5571
### **Python3**

solution/1600-1699/1682.Longest Palindromic Subsequence II/README_EN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@
4444

4545
## Solutions
4646

47+
**Solution 1: Memorization Search**
48+
49+
We design a function $dfs(i, j, x)$ to represent the length of the longest "good" palindrome subsequence ending with character $x$ in the index range $[i, j]$ of string $s$. The answer is $dfs(0, n - 1, 26)$.
50+
51+
The calculation process of the function $dfs(i, j, x)$ is as follows:
52+
53+
- If $i >= j$, then $dfs(i, j, x) = 0$;
54+
- If $s[i] = s[j]$ and $s[i] \neq x$, then $dfs(i, j, x) = dfs(i + 1, j - 1, s[i]) + 2$;
55+
- If $s[i] \neq s[j]$, then $dfs(i, j, x) = max(dfs(i + 1, j, x), dfs(i, j - 1, x))$.
56+
57+
During the process, we can use memorization search to avoid repeated calculations.
58+
59+
The time complexity is $O(n^2 \times C)$. Where $n$ is the length of the string $s$, and $C$ is the size of the character set. In this problem, $C = 26$.
60+
4761
<!-- tabs:start -->
4862

4963
### **Python3**

0 commit comments

Comments
 (0)