Max Increase to Keep City Skyline (LeetCode 807): Solution and Explanation

Medium
Array
Greedy
Matrix

Each building sits in one row skyline and one column skyline. It can grow up to the smaller of those two maxima, and raising every building to that limit keeps both views unchanged.

Intuition

Seen from the side, a row looks as tall as its tallest building. Seen from the front, a column looks as tall as its tallest building. Those are the only two views a building affects.

If a building grew above its row maximum, the side skyline would change. If it grew above its column maximum, the front skyline would change. So its ceiling is min(rowMax[r], colMax[c]).

Raising one building to its ceiling never raises any row or column maximum, because the ceiling is at most both. So every building can be raised independently, and the answer is the sum of all the gaps.

Approach: Row and column maxima

  1. Compute rowMax[r], the largest value in each row.
  2. Compute colMax[c], the largest value in each column.
  3. For every cell, add min(rowMax[r], colMax[c]) - grid[r][c] to total.
  4. Return total.

Step-by-step example

grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
  1. Row maxima (side skyline): [8, 7, 9, 3].
  2. Column maxima (front skyline): [9, 4, 8, 7].
  3. Row 0: limits min(8, [9, 4, 8, 7]) = [8, 4, 8, 7], heights [3, 0, 8, 4], increase 5 + 4 + 0 + 3 = 12.
  4. Row 1: limits [7, 4, 7, 7], increase 5 + 0 + 2 + 0 = 7. Row 2: limits [9, 4, 8, 7], increase 0 + 2 + 2 + 4 = 8. Row 3: limits [3, 3, 3, 3], increase 3 + 0 + 2 + 3 = 8.
  5. Total: 12 + 7 + 8 + 8 = 35.

Code: Python, Java, C++

1class Solution:
2    def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
3        n = len(grid)
4        row_max = [max(row) for row in grid]
5        col_max = [max(grid[r][c] for r in range(n)) for c in range(n)]
6        total = 0
7        for r in range(n):
8            for c in range(n):
9                total += min(row_max[r], col_max[c]) - grid[r][c]
10        return total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n^2)

One pass over the grid for the maxima and one more for the sum.

Space

O(n)

Two arrays of n maxima.

Other approaches

Recompute maxima per cell (time O(n^3), space O(1))

For each cell, scan its row and column to find the two maxima. Correct but repeats work.

Single combined pass for maxima (time O(n^2), space O(n))

Update rowMax[r] and colMax[c] together while visiting each cell once, then do the summing pass. Same complexity, one fewer loop.

Common mistakes

  • Using max instead of min of the two limits. The building has to respect both skylines.
  • Updating the grid while computing maxima. Compute both maxima arrays first from the original heights.
  • Worrying that one raise breaks another. It cannot, since no building is raised above any existing maximum.

Frequently asked questions

Why are only two of the four directions considered?

The skyline from the north is the same as from the south (column maxima), and east is the same as west (row maxima). So there are only two distinct views.

Can raising one building change another building's limit?

No. Each new height is at most its row and column maxima, so no maximum changes and every other limit stays the same.

Is a greedy choice really optimal here?

Yes. Each building's limit is fixed by the original maxima and is independent of the others, so taking every building to its limit gives the largest possible total.