Leetcode Weekly Competition 2386. Find the Kth largest sum of an array-java implementation

Topic category

Huawei school recruitment

Link to original title

You are given an integer array nums and a positive integer k. You can select any subsequence of the array and sum all its elements.

The kth largest sum of an array is defined as: the kth largest subsequence sum that can be obtained (subsequence sums allow repetitions)

Returns the kth largest sum of an array.

A subsequence is an array that can be derived from other arrays by deleting some or none of its elements, and the derivation process does not change the order of the remaining elements.

Note: The sum of empty subsequences is treated as 0.

Code example: Input: nums = [2,4,-2], k = 5
Output: 2
Explanation: All possible obtained subsequence sums are listed below, in descending order:


  • The 5th largest sum of the 6, 4, 4, 2, 2, 0, 0, -2 array is 2.

answer

The problem-solving video of station B
insert image description here
can actually be done with the largest heap. In essence, it is to find the Sum of all positive numbers and
then ask the sum of the Kth largest subsequence.
It is to subtract the sum of the k-1 smallest non-empty subsequences from the Sum
. If it is a minimum heap, then the final result is sum -
if kSum is a maximum heap, then it is directly the top element of the heap

Specifically, the max-heap maintains the sum of the subsequences, and the index i of the numbers (subsequent to be subtracted).

Initially, put sum and subscript 0 into the heap.

max heap

Minimum heap
. The minimum heap I wrote by myself failed to pass the maximum heap. I don’t know where there is a problem. I can’t see it. If you can see it, you can point out the mistakes.

class Solution {
    
    
    public long kSum(int[] nums, int k) {
    
    
        long Sum = 0l;
        for(int a : nums){
    
    
            if(a >= 0) Sum += a ;
            else a = -a ;
        }
        Arrays.sort(nums);
        PriorityQueue<long[]> q = new  PriorityQueue<>((a,b)->(int)(a[0]-b[0]));
        q.offer(new long[]{
    
    nums[0],0});
     //k--;
        while(--k > 0){
    
    
            long[] t = q.poll();
            int i = (int) t[1];
            if(i + 1 < nums.length){
    
    
                q .offer(new long[]{
    
    t[0] + nums[i + 1] - nums[i], i + 1});
                // 上一个下标的数要选
                q .offer(new long[]{
    
    t[0] + nums[i + 1], i + 1});
 
               // q.offer(new long[]{t[0]+nums[i],i+1});
               // q.offer(new long[]{t[0] + nums[i]-nums[i-1],i+1});//不选
            }
        }
        return Sum-q.peek()[0];
 
    }
}

The problem is the summation, but I feel that the two summation methods are the same.

 
 class Solution {
    
    
    public long kSum(int[] nums, int k) {
    
    
        long sum = 0l;
        // for(int a : nums){
    
    
        //     if(a >= 0) Sum += a ;
        //     else a = -a ;
        // }
        for (var i = 0; i < nums.length; i++)
            if (nums[i] >= 0) sum += nums[i];
            else nums[i] = -nums[i];
        Arrays.sort(nums);
        PriorityQueue<long[]> q = new  PriorityQueue<>((a,b)->(int)(b[0]-a[0]));
        q.offer(new long[]{
    
    sum,0});
        while(--k > 0){
    
    
            long[] t = q.poll();
            long s = t[0] ;
            int i = (int) t[1];
            if(i  < nums.length){
    
                    
               q.offer(new long[]{
    
    s-nums[i],i+1});
                if(i > 0) q.offer(new long[]{
    
    s - nums[i]+nums[i-1],i+1});//不选
            }
        }
        return q.peek()[0];
         
 
 
    }
}

Guess you like

Origin blog.csdn.net/qq_41810415/article/details/130321131