Skip to main content
Back to problems
#3402
Easy Algorithms

Minimum operations to make columns strictly increasing

Array Greedy Matrix
72.6% acceptance
Feb 25, 2026
65
5
You are given a m x n matrix grid consisting of non-negative integers. In one operation, you can increment the value of any grid[i][j] by 1. Return the minimum number of operations needed to make all columns of grid strictly increasing.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn minimum_operations(grid: Vec<Vec<i32>>) -> i32 {
    let m = grid.len();
    let n = grid[0].len();
    let mut grid = grid;
    let mut ops = 0;
    for j in 0..n {
      for i in 1..m {
        if grid[i][j] <= grid[i - 1][j] {
          ops += grid[i - 1][j] + 1 - grid[i][j];
          grid[i][j] = grid[i - 1][j] + 1;
        }
      }
    }
    ops
  }
}