Skip to content

Commit 4bd9636

Browse files
committed
Add solution #3462
1 parent 2bad572 commit 4bd9636

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@
843843
3402|[Minimum Operations to Make Columns Strictly Increasing](./3402-minimum-operations-to-make-columns-strictly-increasing.js)|Easy|
844844
3452|[Sum of Good Numbers](./3452-sum-of-good-numbers.js)|Easy|
845845
3461|[Check If Digits Are Equal in String After Operations I](./3461-check-if-digits-are-equal-in-string-after-operations-i.js)|Easy|
846+
3462|[Maximum Sum With at Most K Elements](./3462-maximum-sum-with-at-most-k-elements.js)|Medium|
846847

847848
## License
848849

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 3462. Maximum Sum With at Most K Elements
3+
* https://leetcode.com/problems/maximum-sum-with-at-most-k-elements/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 2D integer matrix grid of size n x m, an integer array limits of length n, and
7+
* an integer k. The task is to find the maximum sum of at most k elements from the matrix grid
8+
* such that:
9+
* - The number of elements taken from the ith row of grid does not exceed limits[i].
10+
*
11+
* Return the maximum sum.
12+
*/
13+
14+
/**
15+
* @param {number[][]} grid
16+
* @param {number[]} limits
17+
* @param {number} k
18+
* @return {number}
19+
*/
20+
var maxSum = function(grid, limits, k) {
21+
const result = [];
22+
grid.forEach((row, i) => {
23+
result.push(...row.slice().sort((a, b) => b - a).slice(0, limits[i]));
24+
});
25+
return result.sort((a, b) => b - a).slice(0, k).reduce((sum, num) => sum + num, 0);
26+
};

0 commit comments

Comments
 (0)