Skip to main content
Back to problems
#2500
Easy Algorithms

Delete greatest value in each row

Array Sorting Heap (Priority Queue) Matrix Simulation
79.8% acceptance
Feb 25, 2026
705
53
You are given an m x n matrix grid of positive integers. Each step: delete the max from each row, add the overall max of deleted to answer. Return the total answer.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn delete_greatest_value(mut grid: Vec<Vec<i32>>) -> i32 {
    for row in grid.iter_mut() { row.sort(); }
    let m = grid.len();
    let n = grid[0].len();
    let mut ans = 0;
    for j in (0..n).rev() {
      let col_max = (0..m).map(|i| grid[i][j]).max().unwrap();
      ans += col_max;
    }
    ans
  }
}