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.
Example 1:
Input: grid = [[3,2],[1,3],[3,4],[0,1]]
Output: 15
Explanation:
0th column strictly increasing, we can apply 3 operations on grid[1][0], 2 operations on grid[2][0], and 6 operations on grid[3][0].1st column strictly increasing, we can apply 4 operations on grid[3][1].
Example 2:
Input: grid = [[3,2,1],[2,1,0],[1,2,3]]
Output: 12
Explanation:
0th column strictly increasing, we can apply 2 operations on grid[1][0], and 4 operations on grid[2][0].1st column strictly increasing, we can apply 2 operations on grid[1][1], and 2 operations on grid[2][1].2nd column strictly increasing, we can apply 2 operations on grid[1][2].
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 500 <= grid[i][j] < 2500
Problem Overview: You are given an m x n grid of integers. You can increment any cell by 1 in a single operation. The goal is to make every column strictly increasing from top to bottom while performing the minimum number of operations.
Approach 1: Increment Simulation (Brute Force) (Time: O(m * n * k), Space: O(1))
The straightforward idea is to process each column and repeatedly increment elements until the column becomes strictly increasing. For every cell grid[r][c], compare it with the element above it. If it is not greater, keep incrementing the current cell until it becomes prev + 1. Each increment is counted as a separate operation. This approach works but can perform many unnecessary repeated increments when the difference between values is large.
Because increments are simulated one-by-one, the effective complexity becomes O(m * n * k), where k represents the number of increments applied. While simple to reason about, it is inefficient for large values and mainly useful as a conceptual starting point.
Approach 2: Column-wise Greedy Calculation (Time: O(m * n), Space: O(1))
A more efficient strategy is to compute the required increments directly instead of simulating them. Iterate column by column. For each column, keep track of the previous value (the value in the row above after adjustments). If the current cell is already greater than the previous value, move on. Otherwise, calculate how much it must increase to become prev + 1.
Add the difference (prev + 1 - current) to the operation count and treat the cell's adjusted value as prev + 1. This greedy rule ensures the smallest possible increment that maintains the strictly increasing property. Each cell is processed once, making the algorithm linear in the number of elements.
This approach works because increasing a value more than necessary would only increase future constraints in the column. The optimal move is always the minimal increment that satisfies the order requirement.
Recommended for interviews: The greedy column-wise scan is the expected solution. It demonstrates understanding of arrays, matrix traversal, and a simple greedy optimization. Explaining the brute force idea first and then optimizing it to a direct calculation shows strong problem-solving progression.
We can traverse the matrix column by column. For each column, we calculate the minimum number of operations required to make it strictly increasing. Specifically, for each column, we maintain a variable pre to represent the value of the previous element in the current column. Then, we traverse the current column from top to bottom. For the current element cur, if pre < cur, it means the current element is already greater than the previous element, so we only need to update pre = cur. Otherwise, we need to increase the current element to pre + 1 and add the number of increases to the answer.
The time complexity is O(m times n), where m and n are the number of rows and columns of the matrix grid, respectively. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Increment Simulation (Brute Force) | O(m * n * k) | O(1) | Conceptual baseline when first reasoning about the problem |
| Column-wise Greedy Calculation | O(m * n) | O(1) | Optimal solution for interviews and production code |
LeetCode#3402 Minimum Operations to Make Columns Strictly Increasing - Python • CodeJulian • 1,447 views views
Watch 3 more video solutions →Practice Minimum Operations to Make Columns Strictly Increasing with our built-in code editor and test cases.
Practice on FleetCode