3478. Choose K Elements With Maximum Sum
Description
You are given two integer arrays, nums1 and nums2, both of length n, along with a positive integer k.
For each index i from 0 to n - 1, perform the following:
- Find all indices 
jwherenums1[j]is less thannums1[i]. - Choose at most 
kvalues ofnums2[j]at these indices to maximize the total sum. 
Return an array answer of size n, where answer[i] represents the result for the corresponding index i.
Example 1:
Input: nums1 = [4,2,1,5,3], nums2 = [10,20,30,40,50], k = 2
Output: [80,30,0,80,50]
Explanation:
- For 
i = 0: Select the 2 largest values fromnums2at indices[1, 2, 4]wherenums1[j] < nums1[0], resulting in50 + 30 = 80. - For 
i = 1: Select the 2 largest values fromnums2at index[2]wherenums1[j] < nums1[1], resulting in 30. - For 
i = 2: No indices satisfynums1[j] < nums1[2], resulting in 0. - For 
i = 3: Select the 2 largest values fromnums2at indices[0, 1, 2, 4]wherenums1[j] < nums1[3], resulting in50 + 30 = 80. - For 
i = 4: Select the 2 largest values fromnums2at indices[1, 2]wherenums1[j] < nums1[4], resulting in30 + 20 = 50. 
Example 2:
Input: nums1 = [2,2,2,2], nums2 = [3,1,2,3], k = 1
Output: [0,0,0,0]
Explanation:
Since all elements in nums1 are equal, no indices satisfy the condition nums1[j] < nums1[i] for any i, resulting in 0 for all positions.
Constraints:
n == nums1.length == nums2.length1 <= n <= 1051 <= nums1[i], nums2[i] <= 1061 <= k <= n
Solutions
Solution 1: Sorting + Priority Queue (Min-Heap)
We can convert the array \(\textit{nums1}\) into an array \(\textit{arr}\), where each element is a tuple \((x, i)\), representing the value \(x\) at index \(i\) in \(\textit{nums1}\). Then, we sort the array \(\textit{arr}\) in ascending order by \(x\).
We use a min-heap \(\textit{pq}\) to maintain the elements from the array \(\textit{nums2}\). Initially, \(\textit{pq}\) is empty. We use a variable \(\textit{s}\) to record the sum of the elements in \(\textit{pq}\). Additionally, we use a pointer \(j\) to maintain the current position in the array \(\textit{arr}\) that needs to be added to \(\textit{pq}\).
We traverse the array \(\textit{arr}\). For the \(h\)-th element \((x, i)\), we add all elements \(\textit{nums2}[\textit{arr}[j][1]]\) to \(\textit{pq}\) that satisfy \(j < h\) and \(\textit{arr}[j][0] < x\), and add these elements to \(\textit{s}\). If the size of \(\textit{pq}\) exceeds \(k\), we pop the smallest element from \(\textit{pq}\) and subtract it from \(\textit{s}\). Then, we update the value of \(\textit{ans}[i]\) to \(\textit{s}\).
After traversing, we return the answer array \(\textit{ans}\).
The time complexity is \(O(n \log n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  |  | 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28  |  | 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30  |  | 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37  |  | 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  |  |