Skip to content

Commit 22044d2

Browse files
authored
feat: add solutions to lc problems (#3498)
1 parent 39f3860 commit 22044d2

File tree

8 files changed

+68
-32
lines changed

8 files changed

+68
-32
lines changed

solution/2300-2399/2386.Find the K-Sum of an Array/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ tags:
7979

8080
由于数组是从小到大排序,这种方式能够不重不漏地按序遍历完所有的子序列和。
8181

82-
时间复杂度 $O(n \times \log n + k \times \log k)$。其中 $n$ 是数组 `nums` 的长度,而 $k$ 是题目中给定的 $k$。
82+
时间复杂度 $O(n \times \log n + k \times \log k)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(n)$。
8383

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

solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ The 5-Sum of the array is 2.
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1: Priority Queue (Min Heap)
68+
### Solution 1: Priority Queue (Min-Heap)
6969

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.
7171

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.
7373

74-
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$.
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.
7575

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.
7777

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.
7979

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)$.
8181

8282
<!-- tabs:start -->
8383

solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ tags:
6565

6666
我们二分枚举矩阵的元素 $x$,统计网格中大于该元素的个数 $cnt$,如果 $cnt \ge target$,说明中位数在 $x$ 的左侧(包含 $x$),否则在右侧。
6767

68-
时间复杂度 $O(m\times \log n \times log M)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为网格的行数和列数而 $M$ 为网格中的最大元素。
68+
时间复杂度 $O(m\times \log n \times log M)$,其中 $m$ 和 $n$ 分别为网格的行数和列数而 $M$ 为网格中的最大元素。空间复杂度 $O(1)$
6969

7070
<!-- tabs:start -->
7171

solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ tags:
5757

5858
<!-- solution:start -->
5959

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)$.
6167

6268
<!-- tabs:start -->
6369

solution/2300-2399/2389.Longest Subsequence With Limited Sum/README_EN.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ tags:
6363

6464
<!-- solution:start -->
6565

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.
6773

6874
<!-- tabs:start -->
6975

@@ -229,7 +235,19 @@ public class Solution {
229235

230236
<!-- solution:start -->
231237

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.
233251

234252
<!-- tabs:start -->
235253

solution/2300-2399/2390.Removing Stars From a String/README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ tags:
8181

8282
最后我们将栈中元素拼接成字符串返回即可。
8383

84-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
84+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。忽略答案字符串的空间消耗,空间复杂度 $O(1)$
8585

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

@@ -195,15 +195,17 @@ class Solution {
195195
* @return String
196196
*/
197197
function removeStars($s) {
198-
$rs = [];
199-
for ($i = 0; $i < strlen($s); $i++) {
200-
if ($s[$i] == '*') {
201-
array_pop($rs);
198+
$ans = [];
199+
$n = strlen($s);
200+
for ($i = 0; $i < $n; $i++) {
201+
$c = $s[$i];
202+
if ($c === '*') {
203+
array_pop($ans);
202204
} else {
203-
array_push($rs, $s[$i]);
205+
$ans[] = $c;
204206
}
205207
}
206-
return join($rs);
208+
return implode('', $ans);
207209
}
208210
}
209211
```

solution/2300-2399/2390.Removing Stars From a String/README_EN.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ There are no more stars, so we return &quot;lecoe&quot;.</pre>
7373

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

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)$.
7783

7884
<!-- tabs:start -->
7985

@@ -187,15 +193,17 @@ class Solution {
187193
* @return String
188194
*/
189195
function removeStars($s) {
190-
$rs = [];
191-
for ($i = 0; $i < strlen($s); $i++) {
192-
if ($s[$i] == '*') {
193-
array_pop($rs);
196+
$ans = [];
197+
$n = strlen($s);
198+
for ($i = 0; $i < $n; $i++) {
199+
$c = $s[$i];
200+
if ($c === '*') {
201+
array_pop($ans);
194202
} else {
195-
array_push($rs, $s[$i]);
203+
$ans[] = $c;
196204
}
197205
}
198-
return join($rs);
206+
return implode('', $ans);
199207
}
200208
}
201209
```

solution/2300-2399/2390.Removing Stars From a String/Solution.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ class Solution {
44
* @return String
55
*/
66
function removeStars($s) {
7-
$rs = [];
8-
for ($i = 0; $i < strlen($s); $i++) {
9-
if ($s[$i] == '*') {
10-
array_pop($rs);
7+
$ans = [];
8+
$n = strlen($s);
9+
for ($i = 0; $i < $n; $i++) {
10+
$c = $s[$i];
11+
if ($c === '*') {
12+
array_pop($ans);
1113
} else {
12-
array_push($rs, $s[$i]);
14+
$ans[] = $c;
1315
}
1416
}
15-
return join($rs);
17+
return implode('', $ans);
1618
}
1719
}

0 commit comments

Comments
 (0)