-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2500-delete-greatest-value-in-each-row.js
More file actions
38 lines (35 loc) · 1.03 KB
/
2500-delete-greatest-value-in-each-row.js
File metadata and controls
38 lines (35 loc) · 1.03 KB
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
/**
* 2500. Delete Greatest Value in Each Row
* https://leetcode.com/problems/delete-greatest-value-in-each-row/
* Difficulty: Easy
*
* You are given an m x n matrix grid consisting of positive integers.
*
* Perform the following operation until grid becomes empty:
* - Delete the element with the greatest value from each row. If multiple such elements exist,
* delete any of them.
* - Add the maximum of deleted elements to the answer.
*
* Note that the number of columns decreases by one after each operation.
*
* Return the answer after performing the operations described above.
*/
/**
* @param {number[][]} grid
* @return {number}
*/
var deleteGreatestValue = function(grid) {
for (const row of grid) {
row.sort((a, b) => a - b);
}
let result = 0;
const cols = grid[0].length;
for (let col = cols - 1; col >= 0; col--) {
let maxInColumn = 0;
for (let row = 0; row < grid.length; row++) {
maxInColumn = Math.max(maxInColumn, grid[row][col]);
}
result += maxInColumn;
}
return result;
};