3462. Maximum Sum With at Most K Elements
Description
You are given a 2D integer matrix grid of size n x m, an integer array limits of length n, and an integer k. The task is to find the maximum sum of at most k elements from the matrix grid such that:
-
The number of elements taken from the
ithrow ofgriddoes not exceedlimits[i].
Return the maximum sum.
Example 1:
Input: grid = [[1,2],[3,4]], limits = [1,2], k = 2
Output: 7
Explanation:
- From the second row, we can take at most 2 elements. The elements taken are 4 and 3.
- The maximum possible sum of at most 2 selected elements is
4 + 3 = 7.
Example 2:
Input: grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3
Output: 21
Explanation:
- From the first row, we can take at most 2 elements. The element taken is 7.
- From the second row, we can take at most 2 elements. The elements taken are 8 and 6.
- The maximum possible sum of at most 3 selected elements is
7 + 8 + 6 = 21.
Constraints:
n == grid.length == limits.lengthm == grid[i].length1 <= n, m <= 5000 <= grid[i][j] <= 1050 <= limits[i] <= m0 <= k <= min(n * m, sum(limits))
Solutions
Solution 1: Greedy + Priority Queue (Min-Heap)
Thinking
Each row may contribute at most \(\textit{limits}[i]\) entries, and we take at most \(k\) in total. The largest admissible cells should be chosen.
Sort each row, keep its \(\textit{limit}\) largest values, then pick the \(k\) largest among those candidates.
A min-heap of size \(k\) receives the row candidates from large to small and evicts the smallest when it overflows. The heap sum is the answer.
We can use a priority queue (min-heap) \(\textit{pq}\) to maintain the largest \(k\) elements.
Traverse each row, sort the elements in each row, and then take the largest \(\textit{limit}\) elements from each row and add them to \(\textit{pq}\). If the size of \(\textit{pq}\) exceeds \(k\), pop the top element of the heap.
Finally, sum the elements in \(\textit{pq}\).
The time complexity is \(O(n \times m \times (\log m + \log k))\), and the space complexity is \(O(k)\). Here, \(n\) and \(m\) are the number of rows and columns of the matrix \(\textit{grid}\), respectively.
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
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 31 32 33 34 35 36 37 38 39 40 41 42 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |