Max Increase to Keep City Skyline
pupil · 405 · lc medium +24 · verified · 86.4% accepted · 2,665 likes · top 97%
Description
A city consists of an n x n block grid, where each block holds one building shaped as a vertical prism. You are given a 0-indexed n x n matrix grid where grid[r][c] is the height of the building at row r, column c.
When the city is viewed from a distance in any direction (north, east, south, or west), it has a skyline — the outer silhouette formed by the tallest buildings in each line of sight.
You may increase the height of any building by any amount (including buildings with height 0). However, no building may be raised in a way that changes the skyline from any of the four cardinal directions.
Return the maximum total increase in building heights achievable under this constraint.
Example 1:
Example 2:
Code