Skip to content
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

feat: update solutions to lc problem: No.2386 #2420

Merged
merged 1 commit into from
Mar 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
19 changes: 9 additions & 10 deletions solution/2300-2399/2386.Find the K-Sum of an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

每次取出堆顶,并放入两种新情况:一是再选择下一位,二是选择下一位并且不选择本位。

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

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

Expand All @@ -69,11 +69,11 @@
class Solution:
def kSum(self, nums: List[int], k: int) -> int:
mx = 0
for i, v in enumerate(nums):
if v > 0:
mx += v
for i, x in enumerate(nums):
if x > 0:
mx += x
else:
nums[i] = -v
nums[i] = -x
nums.sort()
h = [(0, 0)]
for _ in range(k - 1):
Expand Down Expand Up @@ -118,8 +118,6 @@ class Solution {
```

```cpp
using pli = pair<long long, int>;

class Solution {
public:
long long kSum(vector<int>& nums, int k) {
Expand All @@ -133,6 +131,7 @@ public:
}
}
sort(nums.begin(), nums.end());
using pli = pair<long long, int>;
priority_queue<pli, vector<pli>, greater<pli>> pq;
pq.push({0, 0});
while (--k) {
Expand All @@ -155,9 +154,9 @@ public:
```go
func kSum(nums []int, k int) int64 {
mx := 0
for i, v := range nums {
if v > 0 {
mx += v
for i, x := range nums {
if x > 0 {
mx += x
} else {
nums[i] *= -1
}
Expand Down
31 changes: 21 additions & 10 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 @@ -47,19 +47,31 @@ The 5-Sum of the array is 2.

## Solutions

### Solution 1
### Solution 1: Priority Queue (Min Heap)

First, we find the maximum subsequence 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.

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

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.

Since the array is sorted from small to large, this method can traverse all subsequence sums in order without duplication.

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.

<!-- tabs:start -->

```python
class Solution:
def kSum(self, nums: List[int], k: int) -> int:
mx = 0
for i, v in enumerate(nums):
if v > 0:
mx += v
for i, x in enumerate(nums):
if x > 0:
mx += x
else:
nums[i] = -v
nums[i] = -x
nums.sort()
h = [(0, 0)]
for _ in range(k - 1):
Expand Down Expand Up @@ -104,8 +116,6 @@ class Solution {
```

```cpp
using pli = pair<long long, int>;

class Solution {
public:
long long kSum(vector<int>& nums, int k) {
Expand All @@ -119,6 +129,7 @@ public:
}
}
sort(nums.begin(), nums.end());
using pli = pair<long long, int>;
priority_queue<pli, vector<pli>, greater<pli>> pq;
pq.push({0, 0});
while (--k) {
Expand All @@ -141,9 +152,9 @@ public:
```go
func kSum(nums []int, k int) int64 {
mx := 0
for i, v := range nums {
if v > 0 {
mx += v
for i, x := range nums {
if x > 0 {
mx += x
} else {
nums[i] *= -1
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using pli = pair<long long, int>;

class Solution {
public:
long long kSum(vector<int>& nums, int k) {
Expand All @@ -13,6 +11,7 @@ class Solution {
}
}
sort(nums.begin(), nums.end());
using pli = pair<long long, int>;
priority_queue<pli, vector<pli>, greater<pli>> pq;
pq.push({0, 0});
while (--k) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
func kSum(nums []int, k int) int64 {
mx := 0
for i, v := range nums {
if v > 0 {
mx += v
for i, x := range nums {
if x > 0 {
mx += x
} else {
nums[i] *= -1
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Solution:
def kSum(self, nums: List[int], k: int) -> int:
mx = 0
for i, v in enumerate(nums):
if v > 0:
mx += v
for i, x in enumerate(nums):
if x > 0:
mx += x
else:
nums[i] = -v
nums[i] = -x
nums.sort()
h = [(0, 0)]
for _ in range(k - 1):
Expand Down
Loading