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 6717e38aead5c..22e8819dcfce8 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 @@ -59,7 +59,7 @@ 每次取出堆顶,并放入两种新情况:一是再选择下一位,二是选择下一位并且不选择本位。 -由于数组是从小到大排序,可以证明,这种方式能够不重不漏地按序遍历完所有的子序列和。 +由于数组是从小到大排序,这种方式能够不重不漏地按序遍历完所有的子序列和。 时间复杂度 $O(n \times \log n + k \times \log k)$。其中 $n$ 是数组 `nums` 的长度,而 $k$ 是题目中给定的 $k$。 @@ -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): @@ -118,8 +118,6 @@ class Solution { ``` ```cpp -using pli = pair; - class Solution { public: long long kSum(vector& nums, int k) { @@ -133,6 +131,7 @@ public: } } sort(nums.begin(), nums.end()); + using pli = pair; priority_queue, greater> pq; pq.push({0, 0}); while (--k) { @@ -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 } 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 0881cff627a8f..fb3a344eadfce 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 @@ -47,7 +47,19 @@ 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. @@ -55,11 +67,11 @@ The 5-Sum of the array is 2. 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): @@ -104,8 +116,6 @@ class Solution { ``` ```cpp -using pli = pair; - class Solution { public: long long kSum(vector& nums, int k) { @@ -119,6 +129,7 @@ public: } } sort(nums.begin(), nums.end()); + using pli = pair; priority_queue, greater> pq; pq.push({0, 0}); while (--k) { @@ -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 } diff --git a/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.cpp b/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.cpp index dfa06b8b06139..1ba86d7478202 100644 --- a/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.cpp +++ b/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.cpp @@ -1,5 +1,3 @@ -using pli = pair; - class Solution { public: long long kSum(vector& nums, int k) { @@ -13,6 +11,7 @@ class Solution { } } sort(nums.begin(), nums.end()); + using pli = pair; priority_queue, greater> pq; pq.push({0, 0}); while (--k) { diff --git a/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.go b/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.go index fadb5bf736d73..6caae5953b962 100644 --- a/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.go +++ b/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.go @@ -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 } diff --git a/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.py b/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.py index 9bf31e4ad80cf..8bfbba8951818 100644 --- a/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.py +++ b/solution/2300-2399/2386.Find the K-Sum of an Array/Solution.py @@ -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):