-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumOperations.js
More file actions
39 lines (34 loc) · 1.33 KB
/
minimumOperations.js
File metadata and controls
39 lines (34 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
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:
To make the 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].
To make the 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:
To make the 0th column strictly increasing, we can apply 2 operations on grid[1][0], and 4 operations on grid[2][0].
To make the 1st column strictly increasing, we can apply 2 operations on grid[1][1], and 2 operations on grid[2][1].
To make the 2nd column strictly increasing, we can apply 2 operations on grid[1][2].
* @param {number[][]} grid
* @return {number}
*/
var minimumOperations = function (grid) {
const m = grid.length,
n = grid[0].length;
let res = 0;
for (let j = 0; j < n; j++) {
for (let i = 1; i < m; i++) {
if (grid[i][j] <= grid[i - 1][j]) {
res += grid[i - 1][j] - grid[i][j] + 1;
grid[i][j] = grid[i - 1][j] + 1;
}
}
}
return res;
};