#3478

Choose K Elements With Maximum Sum

medium · 33.3% accepted · 178 likes · top 11%

array · sorting · heap (priority queue)

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 j where nums1[j] is less than nums1[i].

- Choose at most k values of nums2[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.

Solution