diff --git a/solution/2300-2399/2386.Find the K-Sum of an Array/README.md b/solution/2300-2399/2386.Find the K-Sum of an Array/README.md index 3ec75c6778f57..9efbd946922b4 100644 --- a/solution/2300-2399/2386.Find the K-Sum of an Array/README.md +++ b/solution/2300-2399/2386.Find the K-Sum of an Array/README.md @@ -79,7 +79,7 @@ tags: 由于数组是从小到大排序,这种方式能够不重不漏地按序遍历完所有的子序列和。 -时间复杂度 $O(n \times \log n + k \times \log k)$。其中 $n$ 是数组 `nums` 的长度,而 $k$ 是题目中给定的 $k$。 +时间复杂度 $O(n \times \log n + k \times \log k)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(n)$。 diff --git a/solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md b/solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md index 574d66089667f..bc1c9a1d86fed 100644 --- a/solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md +++ b/solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md @@ -65,19 +65,19 @@ The 5-Sum of the array is 2. -### Solution 1: Priority Queue (Min Heap) +### Solution 1: Priority Queue (Min-Heap) -First, we find the maximum subsequence sum $mx$, which is the sum of all positive numbers. +First, we find the maximum subarray sum $mx$, which is the sum of all positive numbers. -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. +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. -We only need to sort all numbers in ascending order by their absolute values, then establish a min heap, storing pairs $(s, i)$, representing the current sum is $s$, and the index of the next number to be selected is $i$. +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. -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. +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. -Since the array is sorted from small to large, this method can traverse all subsequence sums in order without duplication. +Since the array is sorted in ascending order, this method can traverse all subarray sums in order without omission. -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. +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)$. diff --git a/solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README.md b/solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README.md index acb9b1a892f88..21a9d3e10644c 100644 --- a/solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README.md +++ b/solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README.md @@ -65,7 +65,7 @@ tags: 我们二分枚举矩阵的元素 $x$,统计网格中大于该元素的个数 $cnt$,如果 $cnt \ge target$,说明中位数在 $x$ 的左侧(包含 $x$),否则在右侧。 -时间复杂度 $O(m\times \log n \times log M)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为网格的行数和列数;而 $M$ 为网格中的最大元素。 +时间复杂度 $O(m\times \log n \times log M)$,其中 $m$ 和 $n$ 分别为网格的行数和列数,而 $M$ 为网格中的最大元素。空间复杂度 $O(1)$。 diff --git a/solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README_EN.md b/solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README_EN.md index e07b08518e3ac..9fa2cd23fc6f5 100644 --- a/solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README_EN.md +++ b/solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README_EN.md @@ -57,7 +57,13 @@ tags: -### Solution 1 +### Solution 1: Two Binary Searches + +The median is actually the $target = \left \lceil \frac{m \times n}{2} \right \rceil$-th number after sorting. + +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. + +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)$. diff --git a/solution/2300-2399/2389.Longest Subsequence With Limited Sum/README_EN.md b/solution/2300-2399/2389.Longest Subsequence With Limited Sum/README_EN.md index 88f10ae3083fe..6761f578e85b5 100644 --- a/solution/2300-2399/2389.Longest Subsequence With Limited Sum/README_EN.md +++ b/solution/2300-2399/2389.Longest Subsequence With Limited Sum/README_EN.md @@ -63,7 +63,13 @@ tags: -### Solution 1 +### Solution 1: Sorting + Prefix Sum + Binary Search + +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. + +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. + +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. @@ -229,7 +235,19 @@ public class Solution { -### Solution 2 +### Solution 2: Sorting + Offline Query + Two Pointers + +Similar to Solution 1, we can first sort the array $nums$ in ascending order. + +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. + +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$. + +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. + +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]$. + +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. diff --git a/solution/2300-2399/2390.Removing Stars From a String/README.md b/solution/2300-2399/2390.Removing Stars From a String/README.md index 05ae99299fe41..516cb0ed256b9 100644 --- a/solution/2300-2399/2390.Removing Stars From a String/README.md +++ b/solution/2300-2399/2390.Removing Stars From a String/README.md @@ -81,7 +81,7 @@ tags: 最后我们将栈中元素拼接成字符串返回即可。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。忽略答案字符串的空间消耗,空间复杂度 $O(1)$。 @@ -195,15 +195,17 @@ class Solution { * @return String */ function removeStars($s) { - $rs = []; - for ($i = 0; $i < strlen($s); $i++) { - if ($s[$i] == '*') { - array_pop($rs); + $ans = []; + $n = strlen($s); + for ($i = 0; $i < $n; $i++) { + $c = $s[$i]; + if ($c === '*') { + array_pop($ans); } else { - array_push($rs, $s[$i]); + $ans[] = $c; } } - return join($rs); + return implode('', $ans); } } ``` diff --git a/solution/2300-2399/2390.Removing Stars From a String/README_EN.md b/solution/2300-2399/2390.Removing Stars From a String/README_EN.md index 5d41a59b88995..0b5835a20248f 100644 --- a/solution/2300-2399/2390.Removing Stars From a String/README_EN.md +++ b/solution/2300-2399/2390.Removing Stars From a String/README_EN.md @@ -73,7 +73,13 @@ There are no more stars, so we return "lecoe". -### Solution 1 +### Solution 1: Stack Simulation + +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. + +Finally, concatenate the elements in the stack into a string and return it. + +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)$. @@ -187,15 +193,17 @@ class Solution { * @return String */ function removeStars($s) { - $rs = []; - for ($i = 0; $i < strlen($s); $i++) { - if ($s[$i] == '*') { - array_pop($rs); + $ans = []; + $n = strlen($s); + for ($i = 0; $i < $n; $i++) { + $c = $s[$i]; + if ($c === '*') { + array_pop($ans); } else { - array_push($rs, $s[$i]); + $ans[] = $c; } } - return join($rs); + return implode('', $ans); } } ``` diff --git a/solution/2300-2399/2390.Removing Stars From a String/Solution.php b/solution/2300-2399/2390.Removing Stars From a String/Solution.php index dc1ba3e286738..b6a65d2036bd6 100644 --- a/solution/2300-2399/2390.Removing Stars From a String/Solution.php +++ b/solution/2300-2399/2390.Removing Stars From a String/Solution.php @@ -4,14 +4,16 @@ class Solution { * @return String */ function removeStars($s) { - $rs = []; - for ($i = 0; $i < strlen($s); $i++) { - if ($s[$i] == '*') { - array_pop($rs); + $ans = []; + $n = strlen($s); + for ($i = 0; $i < $n; $i++) { + $c = $s[$i]; + if ($c === '*') { + array_pop($ans); } else { - array_push($rs, $s[$i]); + $ans[] = $c; } } - return join($rs); + return implode('', $ans); } }