Skip to content

2500. Delete Greatest Value in Each Row 👍

  • Time: $O(mn)$
  • Space: $O(\texttt{sort})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  int deleteGreatestValue(vector<vector<int>>& grid) {
    int ans = 0;

    for (vector<int>& row : grid)
      ranges::sort(row);

    for (int j = 0; j < grid[0].size(); ++j) {
      int maxOfColumn = 0;
      for (int i = 0; i < grid.size(); ++i)
        maxOfColumn = max(maxOfColumn, grid[i][j]);
      ans += maxOfColumn;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
  public int deleteGreatestValue(int[][] grid) {
    int ans = 0;

    for (int[] row : grid)
      Arrays.sort(row);

    for (int j = 0; j < grid[0].length; ++j) {
      int maxOfColumn = 0;
      for (int i = 0; i < grid.length; ++i)
        maxOfColumn = Math.max(maxOfColumn, grid[i][j]);
      ans += maxOfColumn;
    }

    return ans;
  }
}
1
2
3
4
5
class Solution:
  def deleteGreatestValue(self, grid: list[list[int]]) -> int:
    for row in grid:
      row.sort()
    return sum(max(col) for col in zip(*grid))