Max Increase to Keep City Skyline

medium arrays matrix greedy

Problem

Given an n x n grid of building heights, you may increase each building's height. The grid's skyline viewed from top/bottom/left/right must remain the same. Return the maximum total sum of increases.

Inputgrid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
Output35
See LeetCode; each cell rises to min(row max, col max).

def maxIncreaseKeepingSkyline(grid):
    n = len(grid)
    row_max = [max(r) for r in grid]
    col_max = [max(grid[i][j] for i in range(n)) for j in range(n)]
    total = 0
    for i in range(n):
        for j in range(n):
            total += min(row_max[i], col_max[j]) - grid[i][j]
    return total
var maxIncreaseKeepingSkyline = function(grid) {
    const n = grid.length;
    const rowMax = grid.map(r => Math.max(...r));
    const colMax = new Array(n).fill(0);
    for (let j = 0; j < n; j++) for (let i = 0; i < n; i++) colMax[j] = Math.max(colMax[j], grid[i][j]);
    let total = 0;
    for (let i = 0; i < n; i++)
        for (let j = 0; j < n; j++)
            total += Math.min(rowMax[i], colMax[j]) - grid[i][j];
    return total;
};
class Solution {
    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int n = grid.length;
        int[] rowMax = new int[n], colMax = new int[n];
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
            rowMax[i] = Math.max(rowMax[i], grid[i][j]);
            colMax[j] = Math.max(colMax[j], grid[i][j]);
        }
        int total = 0;
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
            total += Math.min(rowMax[i], colMax[j]) - grid[i][j];
        return total;
    }
}
class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        int n = grid.size();
        vector<int> rowMax(n, 0), colMax(n, 0);
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
            rowMax[i] = max(rowMax[i], grid[i][j]);
            colMax[j] = max(colMax[j], grid[i][j]);
        }
        int total = 0;
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)
            total += min(rowMax[i], colMax[j]) - grid[i][j];
        return total;
    }
};
Time: O(n^2) Space: O(n)