Class Solution
-
- All Implemented Interfaces:
public final class Solution3462 - Maximum Sum With at Most K Elements.
Medium
You are given a 2D integer matrix
gridof sizen x m, an integer arraylimitsof lengthn, and an integerk. The task is to find the maximum sum of at mostkelements from the matrixgridsuch that:The number of elements taken from the <code>i<sup>th</sup></code> row of
griddoes 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 <= 500<code>0 <= gridj<= 10<sup>5</sup></code>
0 <= limits[i] <= m0 <= k <= min(n * m, sum(limits))
-
-
Constructor Summary
Constructors Constructor Description Solution()
-