LC.P2500[删除每行中的最大值]

方法一:排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int deleteGreatestValue(int[][] grid) {
for (int[] g : grid) {
Arrays.sort(g);
}
int ans = 0, m = grid.length, n = grid[0].length;
for (int j = 0; j < n; ++j) {
int max = -1;
for (int i = 0; i < m; ++i) {
if (grid[i][j] > max) max = grid[i][j];
}
ans += max;
}
return ans;
}
}
  • 时间复杂度:$O(m \times n \times logn)$
  • 空间复杂度:$O()$