[LeetCode] Minimum Operations to Make Columns Strictly Increasing

3402. Minimum Operations to Make Columns Strictly Increasing

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int minimumOperations(vector<vector<int>>& grid) {
int res = 0, n = grid.size(), m = grid[0].size();
for(int i = 1; i < n; i++) {
for(int j = 0; j < m; j++) {
int x = max(grid[i-1][j] + 1, grid[i][j]);
res += x - grid[i][j];
grid[i][j] = x;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/12/30/PS/LeetCode/minimum-operations-to-make-columns-strictly-increasing/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.