comments | difficulty | edit_url | rating | source | tags | |||
---|---|---|---|---|---|---|---|---|
true |
Hard |
2647 |
Weekly Contest 307 Q4 |
|
You are given an integer array nums
and a positive integer k
. You can choose any subsequence of the array and sum all of its elements together.
We define the K-Sum of the array as the kth
largest subsequence sum that can be obtained (not necessarily distinct).
Return the K-Sum of the array.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Note that the empty subsequence is considered to have a sum of 0
.
Example 1:
Input: nums = [2,4,-2], k = 5 Output: 2 Explanation: All the possible subsequence sums that we can obtain are the following sorted in decreasing order: - 6, 4, 4, 2, 2, 0, 0, -2. The 5-Sum of the array is 2.
Example 2:
Input: nums = [1,-2,3,4,-10,12], k = 16 Output: 10 Explanation: The 16-Sum of the array is 10.
Constraints:
n == nums.length
1 <= n <= 105
-109 <= nums[i] <= 109
1 <= k <= min(2000, 2n)
First, we find the maximum subarray 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
We only need to sort all numbers in ascending order by their absolute values, then build a min-heap to store the tuple
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 in ascending order, this method can traverse all subarray sums in order without omission.
The time complexity is
class Solution:
def kSum(self, nums: List[int], k: int) -> int:
mx = 0
for i, x in enumerate(nums):
if x > 0:
mx += x
else:
nums[i] = -x
nums.sort()
h = [(0, 0)]
for _ in range(k - 1):
s, i = heappop(h)
if i < len(nums):
heappush(h, (s + nums[i], i + 1))
if i:
heappush(h, (s + nums[i] - nums[i - 1], i + 1))
return mx - h[0][0]
class Solution {
public long kSum(int[] nums, int k) {
long mx = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
if (nums[i] > 0) {
mx += nums[i];
} else {
nums[i] *= -1;
}
}
Arrays.sort(nums);
PriorityQueue<Pair<Long, Integer>> pq
= new PriorityQueue<>(Comparator.comparing(Pair::getKey));
pq.offer(new Pair<>(0L, 0));
while (--k > 0) {
var p = pq.poll();
long s = p.getKey();
int i = p.getValue();
if (i < n) {
pq.offer(new Pair<>(s + nums[i], i + 1));
if (i > 0) {
pq.offer(new Pair<>(s + nums[i] - nums[i - 1], i + 1));
}
}
}
return mx - pq.peek().getKey();
}
}
class Solution {
public:
long long kSum(vector<int>& nums, int k) {
long long mx = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
if (nums[i] > 0) {
mx += nums[i];
} else {
nums[i] *= -1;
}
}
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) {
auto p = pq.top();
pq.pop();
long long s = p.first;
int i = p.second;
if (i < n) {
pq.push({s + nums[i], i + 1});
if (i) {
pq.push({s + nums[i] - nums[i - 1], i + 1});
}
}
}
return mx - pq.top().first;
}
};
func kSum(nums []int, k int) int64 {
mx := 0
for i, x := range nums {
if x > 0 {
mx += x
} else {
nums[i] *= -1
}
}
sort.Ints(nums)
h := &hp{{0, 0}}
for k > 1 {
k--
p := heap.Pop(h).(pair)
if p.i < len(nums) {
heap.Push(h, pair{p.sum + nums[p.i], p.i + 1})
if p.i > 0 {
heap.Push(h, pair{p.sum + nums[p.i] - nums[p.i-1], p.i + 1})
}
}
}
return int64(mx) - int64((*h)[0].sum)
}
type pair struct{ sum, i int }
type hp []pair
func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool { return h[i].sum < h[j].sum }
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v any) { *h = append(*h, v.(pair)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }