# [LeetCode]#2500. Delete Greatest Value in Each Row

**Environment: Python 3.8**

**Key technique: for, max**

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*.

**Example 1:**

`Input: grid = [[1,2,4],[3,3,1]]`

Output: 8

Explanation: 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.

**Analysis:**

- Find local max value and find global max value in each row.
- Remove local max value in each row.
- Summarize all global max value in each for loop.
- Return summary.

**Solution:**

`class Solution:`

def deleteGreatestValue(self, grid):

ans=0

for i in range(len(grid[0])):

global_max=-1

for j in range(len(grid)):

local_max=max(grid[j])

global_max=max(global_max,local_max)

grid[j].remove(local_max)

ans+=global_max

return ans

**Submissions:**

**Reference:**