Max Increase to Keep City Skyline LeetCode solution in JavaScript
1/**2* @param {number[][]} grid3* @return {number}4*/5var maxIncreaseKeepingSkyline = function (grid) {6let n = grid.length;7let sum = 0;8let cache = [];910for (let i = 0; i < n; i++) {11const rowMax = Math.max(...grid[i]);1213for (let j = 0; j < n; j++) {14let colMax = cache[j];1516if (!colMax) {17let max = Number.MIN_SAFE_INTEGER;18for (let c = 0; c < n; c++) {19max = Math.max(max, grid[c][j]);20}21cache[j] = max;22colMax = max;23}2425sum += Math.min(rowMax, colMax) - grid[i][j];26}27}2829return sum;30};