Skip to content

Commit 7920f29

Browse files
authored
feat: add solutions to lc problems: No.1844+ (doocs#2128)
1 parent 57cdbac commit 7920f29

File tree

14 files changed

+114
-8
lines changed

14 files changed

+114
-8
lines changed

solution/1800-1899/1844.Replace All Digits with Characters/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949

5050
## Solutions
5151

52+
**Solution 1: Simulation**
53+
54+
Traverse the string, for characters at odd indices, replace them with the character that is a certain number of positions after the previous character.
55+
56+
Finally, return the replaced string.
57+
58+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**

solution/1800-1899/1845.Seat Reservation Manager/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ seatManager.unreserve(5); // Unreserve seat 5, so now the available seats are [5
4949

5050
## Solutions
5151

52+
**Solution 1: Priority Queue (Min Heap)**
53+
54+
We can use a priority queue (min heap) to maintain the smallest number of reservable seats.
55+
56+
Initially, put all seat numbers into the priority queue.
57+
58+
When the `reserve` method is called, take out the smallest number from the priority queue, which is the smallest number of reservable seats.
59+
60+
When the `unreserve` method is called, put the seat number back into the priority queue.
61+
62+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of seats.
63+
5264
<!-- tabs:start -->
5365

5466
### **Python3**

solution/1800-1899/1846.Maximum Element After Decreasing and Rearranging/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ arr 中最大元素为 3 。
7171

7272
**方法一:排序 + 贪心**
7373

74+
我们先对数组进行排序,然后将数组的第一个元素设置为 $1$。
75+
76+
接下来,我们从第二个元素开始遍历数组,如果当前元素与前一个元素的差值大于 $1$,我们就贪心地将当前元素减小为前一个元素加 $1$。
77+
78+
最后,我们返回数组中的最大元素。
79+
80+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。
81+
7482
<!-- tabs:start -->
7583

7684
### **Python3**

solution/1800-1899/1846.Maximum Element After Decreasing and Rearranging/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ The largest element in <code>arr is 3.</code>
6363

6464
## Solutions
6565

66+
**Solution 1: Sorting + Greedy Algorithm**
67+
68+
First, we sort the array and then set the first element of the array to $1$.
69+
70+
Next, we start traversing the array from the second element. If the difference between the current element and the previous one is more than $1$, we greedily reduce the current element to the previous element plus $1$.
71+
72+
Finally, we return the maximum element in the array.
73+
74+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the length of the array.
75+
6676
<!-- tabs:start -->
6777

6878
### **Python3**

solution/1800-1899/1847.Closest Room/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ Query = [2,5]: Room number 3 is the only room with a size of at least 5. The ans
5252

5353
## Solutions
5454

55+
**Solution 1: Offline Query + Ordered Set + Binary Search**
56+
57+
We notice that the order of queries does not affect the answer, and the problem involves the size relationship of room areas. Therefore, we can sort the queries in ascending order of minimum area, so that we can process each query from small to large. Also, we sort the rooms in ascending order of area.
58+
59+
Next, we create an ordered list and add all room numbers to the ordered list.
60+
61+
Then, we process each query from small to large. For each query, we first remove all rooms with an area less than or equal to the current query's minimum area from the ordered list. Then, in the remaining rooms, we use binary search to find the room number closest to the current query. If there is no such room, we return $-1$.
62+
63+
The time complexity is $O(n \times \log n + k \times \log k)$, and the space complexity is $O(n + k)$. Where $n$ and $k$ are the number of rooms and queries, respectively.
64+
5565
<!-- tabs:start -->
5666

5767
### **Python3**

solution/1800-1899/1848.Minimum Distance to the Target Element/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555

5656
**方法一:一次遍历**
5757

58-
遍历数组,找到所有等于 `target` 的下标,然后计算 `abs(i - start)`,取最小值即可。
58+
遍历数组,找到所有等于 $target$ 的下标,然后计算 $|i - start|$,取最小值即可。
5959

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

6262
<!-- tabs:start -->
6363

solution/1800-1899/1848.Minimum Distance to the Target Element/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747

4848
## Solutions
4949

50+
**Solution 1: Single Pass**
51+
52+
Traverse the array, find all indices equal to $target$, then calculate $|i - start|$, and take the minimum value.
53+
54+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**

solution/1800-1899/1849.Splitting a String Into Descending Consecutive Values/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ The values are in descending order with adjacent values differing by 1.
5353

5454
## Solutions
5555

56+
**Solution 1: DFS (Depth-First Search)**
57+
58+
Starting from the first character of the string, enumerate all possible split positions. Check if the split substring meets the requirements of the problem. If it does, continue to recursively check whether the remaining substring meets the requirements, until the entire string is traversed.
59+
60+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ is the length of the string.
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**

solution/1800-1899/1851.Minimum Interval to Include Each Query/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@
4848

4949
## Solutions
5050

51+
**Solution 1: Sorting + Offline Query + Priority Queue (Min Heap)**
52+
53+
We notice that the order of queries does not affect the answer, and the intervals involved do not change. Therefore, we consider sorting all queries in ascending order, and sorting all intervals in ascending order of the left endpoint.
54+
55+
We use a priority queue (min heap) $pq$ to maintain all current intervals. Each element in the queue is a pair $(v, r)$, representing an interval with length $v$ and right endpoint $r$. Initially, the priority queue is empty. In addition, we define a pointer $i$ that points to the current interval being traversed, and initially $i=0$.
56+
57+
We traverse each query $(x, j)$ in ascending order and perform the following operations:
58+
59+
- If the pointer $i$ has not traversed all intervals, and the left endpoint of the current interval $[a, b]$ is less than or equal to $x$, then we add this interval to the priority queue and move the pointer $i$ one step forward. Repeat this process.
60+
- If the priority queue is not empty, and the right endpoint of the heap top element is less than $x$, then we pop the heap top element. Repeat this process.
61+
- At this point, if the priority queue is not empty, then the heap top element is the smallest interval containing $x$. We add its length $v$ to the answer array $ans$.
62+
63+
After the above process is over, we return the answer array $ans$.
64+
65+
The time complexity is $O(n \times \log n + m \times \log m)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the lengths of the arrays `intervals` and `queries` respectively.
66+
5167
<!-- tabs:start -->
5268

5369
### **Python3**

solution/1800-1899/1852.Distinct Numbers in Each Subarray/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444

4545
## Solutions
4646

47+
**Solution 1: Sliding Window + Hash Table/Array**
48+
49+
Use an array or hash table to record the occurrence of numbers in each subarray of size $k$. Then traverse the array, update the hash table each time, and record the number of types of numbers in the current window.
50+
51+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
52+
4753
<!-- tabs:start -->
4854

4955
### **Python3**

solution/1800-1899/1854.Maximum Population Year/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ The earlier year between them is 1960.</pre>
3838

3939
## Solutions
4040

41+
**Solution 1: Difference Array**
42+
43+
We notice that the range of years is $[1950,..2050]$. Therefore, we can map these years to an array $d$ of length $101$, where the index of the array represents the value of the year minus $1950$.
44+
45+
Next, we traverse $logs$. For each person, we increment $d[birth_i - 1950]$ by $1$ and decrement $d[death_i - 1950]$ by $1$. Finally, we traverse the array $d$, find the maximum value of the prefix sum, which is the year with the most population, and add $1950$ to get the answer.
46+
47+
The time complexity is $O(n)$, and the space complexity is $O(C)$. Where $n$ is the length of the array $logs$, and $C$ is the range size of the years, i.e., $2050 - 1950 + 1 = 101$.
48+
4149
<!-- tabs:start -->
4250

4351
### **Python3**

solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@
5959

6060
**方法一:二分查找**
6161

62-
假设 `nums1``nums2` 的长度分别为 $m$ 和 $n$。
62+
假设 $nums1$, $nums2$ 的长度分别为 $m$ 和 $n$。
6363

64-
遍历数组 `nums1`,对于每个数字 `nums1[i]`,二分查找 `nums2` 在 $[i,n)$ 范围内的数字,找到**最后一个**大于等于 `nums1[i]` 的位置 $j$,计算此位置与 $i$ 的距离,并更新最大距离值 `ans`
64+
遍历数组 $nums1$,对于每个数字 $nums1[i]$,二分查找 $nums2$ 在 $[i,n)$ 范围内的数字,找到**最后一个**大于等于 $nums1[i]$ 的位置 $j$,计算此位置与 $i$ 的距离,并更新最大距离值 $ans$
6565

66-
时间复杂度 $O(m\log n)$,其中 $m$ 和 $n$ 分别为 `nums1``nums2` 的长度。
66+
时间复杂度 $O(m \times \log n)$,其中 $m$ 和 $n$ 分别为 $nums1$$nums2$ 的长度。空间复杂度 $O(1)$
6767

6868
**方法二:双指针**
6969

70-
在方法一中,我们只利用到 `nums2` 是非递增数组这一条件,实际上,`nums1` 也是非递增数组,我们可以用双指针 $i$ 和 $j$ 来遍历 `nums1``nums2`
70+
在方法一中,我们只利用到 $nums2$ 是非递增数组这一条件,实际上,$nums1$ 也是非递增数组,我们可以用双指针 $i$ 和 $j$ 来遍历 $nums1$$nums2$
7171

72-
时间复杂度 $O(m+n)$。
72+
时间复杂度 $O(m+n)$,其中 $m$ 和 $n$ 分别为 $nums1$ 和 $nums2$ 的长度。空间复杂度 $O(1)$
7373

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

solution/1800-1899/1855.Maximum Distance Between a Pair of Values/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ The maximum distance is 2 with pair (2,4).
5151

5252
## Solutions
5353

54-
Binary search.
54+
**Solution 1: Binary Search**
55+
56+
Assume the lengths of $nums1$ and $nums2$ are $m$ and $n$ respectively.
57+
58+
Traverse array $nums1$, for each number $nums1[i]$, perform a binary search for numbers in $nums2$ in the range $[i,n)$, find the **last** position $j$ that is greater than or equal to $nums1[i]$, calculate the distance between this position and $i$, and update the maximum distance value $ans$.
59+
60+
The time complexity is $O(m \times \log n)$, where $m$ and $n$ are the lengths of $nums1$ and $nums2$ respectively. The space complexity is $O(1)$.
5561

5662
<!-- tabs:start -->
5763

solution/1800-1899/1856.Maximum Subarray Min-Product/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@
5454

5555
## Solutions
5656

57+
**Solution 1: Monotonic Stack + Prefix Sum**
58+
59+
We can enumerate each element $nums[i]$ as the minimum value of the subarray, and find the left and right boundaries $left[i]$ and $right[i]$ of the subarray. Where $left[i]$ represents the first position strictly less than $nums[i]$ on the left side of $i$, and $right[i]$ represents the first position less than or equal to $nums[i]$ on the right side of $i$.
60+
61+
To conveniently calculate the sum of the subarray, we can preprocess the prefix sum array $s$, where $s[i]$ represents the sum of the first $i$ elements of $nums$.
62+
63+
Then the minimum product with $nums[i]$ as the minimum value of the subarray is $nums[i] \times (s[right[i]] - s[left[i] + 1])$. We can enumerate each element $nums[i]$, find the minimum product with $nums[i]$ as the minimum value of the subarray, and then take the maximum value.
64+
65+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$.
66+
5767
<!-- tabs:start -->
5868

5969
### **Python3**

0 commit comments

Comments
 (0)