Skip to content

Commit 5011d97

Browse files
authored
feat: update solutions to lc problem: No.2386 (doocs#2420)
No.2386.Find the K-Sum of an Array
1 parent f325cc5 commit 5011d97

File tree

5 files changed

+38
-29
lines changed

5 files changed

+38
-29
lines changed

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

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

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

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

@@ -69,11 +69,11 @@
6969
class Solution:
7070
def kSum(self, nums: List[int], k: int) -> int:
7171
mx = 0
72-
for i, v in enumerate(nums):
73-
if v > 0:
74-
mx += v
72+
for i, x in enumerate(nums):
73+
if x > 0:
74+
mx += x
7575
else:
76-
nums[i] = -v
76+
nums[i] = -x
7777
nums.sort()
7878
h = [(0, 0)]
7979
for _ in range(k - 1):
@@ -118,8 +118,6 @@ class Solution {
118118
```
119119

120120
```cpp
121-
using pli = pair<long long, int>;
122-
123121
class Solution {
124122
public:
125123
long long kSum(vector<int>& nums, int k) {
@@ -133,6 +131,7 @@ public:
133131
}
134132
}
135133
sort(nums.begin(), nums.end());
134+
using pli = pair<long long, int>;
136135
priority_queue<pli, vector<pli>, greater<pli>> pq;
137136
pq.push({0, 0});
138137
while (--k) {
@@ -155,9 +154,9 @@ public:
155154
```go
156155
func kSum(nums []int, k int) int64 {
157156
mx := 0
158-
for i, v := range nums {
159-
if v > 0 {
160-
mx += v
157+
for i, x := range nums {
158+
if x > 0 {
159+
mx += x
161160
} else {
162161
nums[i] *= -1
163162
}

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

+21-10
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,31 @@ The 5-Sum of the array is 2.
4747

4848
## Solutions
4949

50-
### Solution 1
50+
### Solution 1: Priority Queue (Min Heap)
51+
52+
First, we find the maximum subsequence sum $mx$, which is the sum of all positive numbers.
53+
54+
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.
55+
56+
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$.
57+
58+
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.
59+
60+
Since the array is sorted from small to large, this method can traverse all subsequence sums in order without duplication.
61+
62+
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.
5163

5264
<!-- tabs:start -->
5365

5466
```python
5567
class Solution:
5668
def kSum(self, nums: List[int], k: int) -> int:
5769
mx = 0
58-
for i, v in enumerate(nums):
59-
if v > 0:
60-
mx += v
70+
for i, x in enumerate(nums):
71+
if x > 0:
72+
mx += x
6173
else:
62-
nums[i] = -v
74+
nums[i] = -x
6375
nums.sort()
6476
h = [(0, 0)]
6577
for _ in range(k - 1):
@@ -104,8 +116,6 @@ class Solution {
104116
```
105117

106118
```cpp
107-
using pli = pair<long long, int>;
108-
109119
class Solution {
110120
public:
111121
long long kSum(vector<int>& nums, int k) {
@@ -119,6 +129,7 @@ public:
119129
}
120130
}
121131
sort(nums.begin(), nums.end());
132+
using pli = pair<long long, int>;
122133
priority_queue<pli, vector<pli>, greater<pli>> pq;
123134
pq.push({0, 0});
124135
while (--k) {
@@ -141,9 +152,9 @@ public:
141152
```go
142153
func kSum(nums []int, k int) int64 {
143154
mx := 0
144-
for i, v := range nums {
145-
if v > 0 {
146-
mx += v
155+
for i, x := range nums {
156+
if x > 0 {
157+
mx += x
147158
} else {
148159
nums[i] *= -1
149160
}

solution/2300-2399/2386.Find the K-Sum of an Array/Solution.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using pli = pair<long long, int>;
2-
31
class Solution {
42
public:
53
long long kSum(vector<int>& nums, int k) {
@@ -13,6 +11,7 @@ class Solution {
1311
}
1412
}
1513
sort(nums.begin(), nums.end());
14+
using pli = pair<long long, int>;
1615
priority_queue<pli, vector<pli>, greater<pli>> pq;
1716
pq.push({0, 0});
1817
while (--k) {

solution/2300-2399/2386.Find the K-Sum of an Array/Solution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
func kSum(nums []int, k int) int64 {
22
mx := 0
3-
for i, v := range nums {
4-
if v > 0 {
5-
mx += v
3+
for i, x := range nums {
4+
if x > 0 {
5+
mx += x
66
} else {
77
nums[i] *= -1
88
}

solution/2300-2399/2386.Find the K-Sum of an Array/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
22
def kSum(self, nums: List[int], k: int) -> int:
33
mx = 0
4-
for i, v in enumerate(nums):
5-
if v > 0:
6-
mx += v
4+
for i, x in enumerate(nums):
5+
if x > 0:
6+
mx += x
77
else:
8-
nums[i] = -v
8+
nums[i] = -x
99
nums.sort()
1010
h = [(0, 0)]
1111
for _ in range(k - 1):

0 commit comments

Comments
 (0)