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/2300-2399/2386.Find the K-Sum of an Array/README_EN.md
+7-7
Original file line number
Diff line number
Diff line change
@@ -65,19 +65,19 @@ The 5-Sum of the array is 2.
65
65
66
66
<!-- solution:start -->
67
67
68
-
### Solution 1: Priority Queue (MinHeap)
68
+
### Solution 1: Priority Queue (Min-Heap)
69
69
70
-
First, we find the maximum subsequence sum $mx$, which is the sum of all positive numbers.
70
+
First, we find the maximum subarray sum $mx$, which is the sum of all positive numbers.
71
71
72
-
It can be found that the sum of other subsequences can be regarded as the maximum subsequence sum, minus the sum of other part of the subsequence. Therefore, we can convert the problem into finding the $k$-th smallest subsequence sum.
72
+
It can be observed that the sum of other subarrays can be considered as the maximum subarray sum minus the sum of other parts of the subarray. Therefore, we can convert the problem into finding the $k$-th smallest subarray sum.
73
73
74
-
We only need to sort all numbers in ascending order by their absolute values, then establish a minheap, storing pairs $(s, i)$, representing the current sum is $s$, and the index of the next number to be selected is $i$.
74
+
We only need to sort all numbers in ascending order by their absolute values, then build a min-heap to store the tuple $(s, i)$, where $s$ is the current sum and $i$ is the index of the next number to be selected in the subarray.
75
75
76
-
Each time we take out the top of the heap, and put in two new situations: one is to select the next position, and the other is to select the next position and not select this position.
76
+
Each time, we extract the top of the heap and insert two new situations: one is to select the next number, and the other is to select the next number but not the current number.
77
77
78
-
Since the array is sorted from small to large, this method can traverse all subsequence sums in order without duplication.
78
+
Since the array is sorted in ascending order, this method can traverse all subarray sums in order without omission.
79
79
80
-
The time complexity is $O(n \times \log n + k \times \log k)$, where $n$ is the length of the array `nums`, and $k$ is the given $k$ in the problem.
80
+
The time complexity is $O(n \times \log n + k \times \log k)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(n)$.
Copy file name to clipboardexpand all lines: solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README_EN.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,13 @@ tags:
57
57
58
58
<!-- solution:start -->
59
59
60
-
### Solution 1
60
+
### Solution 1: Two Binary Searches
61
+
62
+
The median is actually the $target = \left \lceil \frac{m \times n}{2} \right \rceil$-th number after sorting.
63
+
64
+
We perform a binary search on the elements of the matrix $x$, counting the number of elements in the grid that are greater than $x$, denoted as $cnt$. If $cnt \ge target$, it means the median is on the left side of $x$ (including $x$); otherwise, it is on the right side.
65
+
66
+
The time complexity is $O(m \times \log n \times \log M)$, where $m$ and $n$ are the number of rows and columns of the grid, respectively, and $M$ is the maximum element in the grid. The space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/2300-2399/2389.Longest Subsequence With Limited Sum/README_EN.md
+20-2
Original file line number
Diff line number
Diff line change
@@ -63,7 +63,13 @@ tags:
63
63
64
64
<!-- solution:start -->
65
65
66
-
### Solution 1
66
+
### Solution 1: Sorting + Prefix Sum + Binary Search
67
+
68
+
According to the problem description, for each $queries[i]$, we need to find a subsequence such that the sum of its elements does not exceed $queries[i]$ and the length of this subsequence is maximized. Obviously, we should choose the smallest possible elements to maximize the length of the subsequence.
69
+
70
+
Therefore, we can first sort the array $nums$ in ascending order. Then, for each $queries[i]$, we can use binary search to find the smallest index $j$ such that $nums[0] + nums[1] + \cdots + nums[j] \gt queries[i]$. At this point, $nums[0] + nums[1] + \cdots + nums[j - 1]$ is the sum of the elements of the subsequence that meets the condition, and the length of this subsequence is $j$. Therefore, we can add $j$ to the answer array.
71
+
72
+
The time complexity is $O((n + m) \times \log n)$, and the space complexity is $O(n)$ or $O(\log n)$. Here, $n$ and $m$ are the lengths of the arrays $nums$ and $queries$, respectively.
67
73
68
74
<!-- tabs:start -->
69
75
@@ -229,7 +235,19 @@ public class Solution {
229
235
230
236
<!-- solution:start -->
231
237
232
-
### Solution 2
238
+
### Solution 2: Sorting + Offline Query + Two Pointers
239
+
240
+
Similar to Solution 1, we can first sort the array $nums$ in ascending order.
241
+
242
+
Next, we define an index array $idx$ of the same length as $queries$, where $idx[i] = i$. Then, we sort the array $idx$ in ascending order based on the values in $queries$. This way, we can process the elements in $queries$ in ascending order.
243
+
244
+
We use a variable $s$ to record the sum of the currently selected elements and a variable $j$ to record the number of currently selected elements. Initially, $s = j = 0$.
245
+
246
+
We traverse the index array $idx$, and for each index $i$ in it, we iteratively add elements from the array $nums$ to the current subsequence until $s + nums[j] \gt queries[i]$. At this point, $j$ is the length of the subsequence that meets the condition. We set the value of $ans[i]$ to $j$ and then continue to process the next index.
247
+
248
+
After traversing the index array $idx$, we obtain the answer array $ans$, where $ans[i]$ is the length of the subsequence that satisfies $queries[i]$.
249
+
250
+
The time complexity is $O(n \times \log n + m)$, and the space complexity is $O(m)$. Here, $n$ and $m$ are the lengths of the arrays $nums$ and $queries$, respectively.
Copy file name to clipboardexpand all lines: solution/2300-2399/2390.Removing Stars From a String/README_EN.md
+15-7
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,13 @@ There are no more stars, so we return "lecoe".</pre>
73
73
74
74
<!-- solution:start -->
75
75
76
-
### Solution 1
76
+
### Solution 1: Stack Simulation
77
+
78
+
We can use a stack to simulate the operation process. Traverse the string $s$, and if the current character is not an asterisk, push it onto the stack; if the current character is an asterisk, pop the top element from the stack.
79
+
80
+
Finally, concatenate the elements in the stack into a string and return it.
81
+
82
+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. Ignoring the space consumption of the answer string, the space complexity is $O(1)$.
0 commit comments