Delete Greatest Value in Each Row

The code sorts each row and then iterates over each column to find the maximum element. It adds this maximum element to the result.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def deleteGreatestValue(self, grid: List[List[int]]) -> int:
        res = 0
        si = len(grid)
        sj = len(grid[0])

        for r in grid:
            r.sort()

        for j in range(sj):
            max_row = 0
            for i in range(si):
                max_row = max(max_row, grid[i][j])
            res += max_row

        return res