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 mostk elements from the matrix grid such that:
The number of elements taken from the ith row of grid does not exceed limits[i].
Input: grid =[[1,2],[3,4]], limits =[1,2], k =2Output: 7Explanation:
* 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`.
Input: grid =[[5,3,7],[8,2,6]], limits =[2,2], k =3Output: 21Explanation:
* From the first row, we can take at most 2 elements. The element taken is7.* 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`.
To maximize the sum, we want to pick the largest possible elements from the matrix, but we must respect the per-row limits and the overall limit of k elements. We can collect the allowed elements from each row, sort them, and pick the top k.
classSolution {
public:int maxSumWithAtMostKElements(vector<vector<int>>& grid, vector<int>& limits, int k) {
vector<int> pool;
for (int i =0; i < grid.size(); ++i) {
auto row = grid[i];
sort(row.rbegin(), row.rend());
for (int j =0; j < min((int)row.size(), limits[i]); ++j)
pool.push_back(row[j]);
}
sort(pool.rbegin(), pool.rend());
int ans =0;
for (int i =0; i < min(k, (int)pool.size()); ++i)
ans += pool[i];
return ans;
}
};
classSolution {
publicintmaxSumWithAtMostKElements(int[][] grid, int[] limits, int k) {
List<Integer> pool =new ArrayList<>();
for (int i = 0; i < grid.length; ++i) {
int[] row = grid[i].clone();
Arrays.sort(row);
for (int j = row.length- 1; j >= row.length- Math.min(row.length, limits[i]); --j)
pool.add(row[j]);
}
pool.sort(Collections.reverseOrder());
int ans = 0;
for (int i = 0; i < Math.min(k, pool.size()); ++i)
ans += pool.get(i);
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
funmaxSumWithAtMostKElements(grid: Array<IntArray>, limits: IntArray, k: Int): Int {
val pool = mutableListOf<Int>()
for (i in grid.indices) {
val row = grid[i].sortedDescending()
for (j in0 until minOf(row.size, limits[i]))
pool.add(row[j])
}
val topK = pool.sortedDescending().take(k)
return topK.sum()
}
}
1
2
3
4
5
6
7
8
classSolution:
defmaxSumWithAtMostKElements(self, grid: list[list[int]], limits: list[int], k: int) -> int:
pool: list[int] = []
for i, row in enumerate(grid):
top = sorted(row, reverse=True)[:limits[i]]
pool.extend(top)
pool.sort(reverse=True)
return sum(pool[:k])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
impl Solution {
pubfnmax_sum_with_at_most_k_elements(grid: Vec<Vec<i32>>, limits: Vec<i32>, k: i32) -> i32 {
letmut pool =vec![];
for (i, row) in grid.iter().enumerate() {
letmut r = row.clone();
r.sort_by(|a, b| b.cmp(a));
for j in0..r.len().min(limits[i] asusize) {
pool.push(r[j]);
}
}
pool.sort_by(|a, b| b.cmp(a));
pool.iter().take(k asusize).sum()
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
maxSumWithAtMostKElements(grid: number[][], limits: number[], k: number):number {
letpool: number[] = [];
for (leti=0; i<grid.length; ++i) {
letrow= [...grid[i]].sort((a, b) =>b-a);
pool.push(...row.slice(0, limits[i]));
}
pool.sort((a, b) =>b-a);
returnpool.slice(0, k).reduce((a, b) =>a+b, 0);
}
}