Max Increase to Keep City Skyline LeetCode solution in JavaScript

1
/**
2
* @param {number[][]} grid
3
* @return {number}
4
*/
5
var maxIncreaseKeepingSkyline = function (grid) {
6
let n = grid.length;
7
let sum = 0;
8
let cache = [];
9

10
for (let i = 0; i < n; i++) {
11
const rowMax = Math.max(...grid[i]);
12

13
for (let j = 0; j < n; j++) {
14
let colMax = cache[j];
15

16
if (!colMax) {
17
let max = Number.MIN_SAFE_INTEGER;
18
for (let c = 0; c < n; c++) {
19
max = Math.max(max, grid[c][j]);
20
}
21
cache[j] = max;
22
colMax = max;
23
}
24

25
sum += Math.min(rowMax, colMax) - grid[i][j];
26
}
27
}
28

29
return sum;
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0