Skip to content

feat: add solutions to lc problems #3498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

<!-- tabs:start -->

Expand Down
14 changes: 7 additions & 7 deletions solution/2300-2399/2386.Find the K-Sum of an Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ The 5-Sum of the array is 2.

<!-- solution:start -->

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)$

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ tags:

<!-- solution:start -->

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ tags:

<!-- solution:start -->

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

<!-- tabs:start -->

Expand Down Expand Up @@ -229,7 +235,19 @@ public class Solution {

<!-- solution:start -->

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

<!-- tabs:start -->

Expand Down
16 changes: 9 additions & 7 deletions solution/2300-2399/2390.Removing Stars From a String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ tags:

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -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);
}
}
```
Expand Down
22 changes: 15 additions & 7 deletions solution/2300-2399/2390.Removing Stars From a String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ There are no more stars, so we return &quot;lecoe&quot;.</pre>

<!-- solution:start -->

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

<!-- tabs:start -->

Expand Down Expand Up @@ -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);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading