
Input: grid =[[1,2,4],[3,3,1]]Output: 8Explanation: The diagram above shows the removed values in each step.- In the first operation, we remove 4 from the first row and 3 from the second row(notice that, there are two cells with value 3 and we can remove any of them). We add 4 to the answer.- In the second operation, we remove 2 from the first row and 3 from the second row. We add 3 to the answer.- In the third operation, we remove 1 from the first row and 1 from the second row. We add 1 to the answer.The final answer =4+3+1=8.

Input: grid =[[10]]Output: 10Explanation: The diagram above shows the removed values in each step.- In the first operation, we remove 10 from the first row. We add 10 to the answer.The final answer =10.
If we sort each row in non-increasing order, then in each operation, the greatest value in each row will always be at the front. For each column (from left to right), the maximum value among all rows in that column is the value added to the answer for that operation.
classSolution {
public:int deleteGreatestValue(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size(), ans =0;
for (auto& row : grid) sort(row.rbegin(), row.rend());
for (int j =0; j < n; ++j) {
int mx =0;
for (int i =0; i < m; ++i) mx = max(mx, grid[i][j]);
ans += mx;
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import"sort"funcdeleteGreatestValue(grid [][]int) int {
m, n:= len(grid), len(grid[0])
fori:=0; i < m; i++ {
sort.Sort(sort.Reverse(sort.IntSlice(grid[i])))
}
ans:=0forj:=0; j < n; j++ {
mx:=0fori:=0; i < m; i++ {
ifgrid[i][j] > mx {
mx = grid[i][j]
}
}
ans+=mx }
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
publicintdeleteGreatestValue(int[][] grid) {
int m = grid.length, n = grid[0].length, ans = 0;
for (int[] row : grid) {
java.util.Arrays.sort(row);
for (int l = 0, r = n - 1; l < r; l++, r--) {
int tmp = row[l]; row[l]= row[r]; row[r]= tmp;
}
}
for (int j = 0; j < n; j++) {
int mx = 0;
for (int i = 0; i < m; i++) mx = Math.max(mx, grid[i][j]);
ans += mx;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
fundeleteGreatestValue(grid: Array<IntArray>): Int {
val m = grid.size
val n = grid[0].size
for (row in grid) row.sortDescending()
var ans = 0for (j in0 until n) {
var mx = 0for (i in0 until m) mx = maxOf(mx, grid[i][j])
ans += mx
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defdeleteGreatestValue(self, grid: list[list[int]]) -> int:
m: int = len(grid)
n: int = len(grid[0])
for row in grid:
row.sort(reverse=True)
ans: int =0for j in range(n):
mx =0for i in range(m):
mx = max(mx, grid[i][j])
ans += mx
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfndelete_greatest_value(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
letmut grid = grid;
for row in&mut grid {
row.sort_by(|a, b| b.cmp(a));
}
letmut ans =0;
for j in0..n {
letmut mx =0;
for i in0..m {
mx = mx.max(grid[i][j]);
}
ans += mx;
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
deleteGreatestValue(grid: number[][]):number {
constm=grid.length, n=grid[0].length;
for (constrowofgrid) row.sort((a, b) =>b-a);
letans=0;
for (letj=0; j<n; j++) {
letmx=0;
for (leti=0; i<m; i++) mx= Math.max(mx, grid[i][j]);
ans+=mx;
}
returnans;
}
}