Minimum Operations to Make Columns Strictly Increasing - Problem
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.
A column is strictly increasing if each element is greater than the element above it: grid[i][j] < grid[i+1][j] for all valid i.
Input & Output
Example 1 — Basic Case
$
Input:
grid = [[3,1,6],[1,1,9],[2,4,7]]
›
Output:
10
💡 Note:
Column 1: [3,1,2] → [3,4,5] (1→4 needs +3, 2→5 needs +3, total 6 ops), Column 2: [1,1,4] → [1,2,4] (1→2 needs +1 op), Column 3: [6,9,7] → [6,9,10] (7→10 needs +3 ops). Total: 10
Example 2 — Already Increasing
$
Input:
grid = [[1,2],[3,4]]
›
Output:
0
💡 Note:
Both columns are already strictly increasing: [1,3] and [2,4], so no operations needed
Example 3 — Single Column
$
Input:
grid = [[5],[4],[3]]
›
Output:
6
💡 Note:
Column [5,4,3] needs to become [5,6,7]: 4→6 (+2 ops), 3→7 (+4 ops). Total: 6 operations
Constraints
- 1 ≤ m, n ≤ 1000
- 0 ≤ grid[i][j] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code