Leetcode 807: Max Increase to Keep City Skyline

grid47
grid47
Exploring patterns and algorithms
Aug 18, 2024 6 min read

A skyline of buildings with the maximum increase calculated, glowing softly as each increase is added.
Solution to LeetCode 807: Max Increase to Keep City Skyline Problem

In a city grid, buildings are represented by an n x n matrix where each element corresponds to the height of a building at a specific location. You can increase the height of any building in the city grid, but the increased height should not alter the skyline of the city when viewed from any of the four cardinal directions (north, south, east, and west). Your task is to calculate the maximum sum of the heights that can be added to the buildings without changing the skyline.
Problem
Approach
Steps
Complexity
Input: The input consists of an n x n grid where each grid[i][j] represents the height of a building at row i and column j.
Example: Input: grid = [[1, 3, 2],[4, 1, 5],[6, 3, 4]]
Constraints:
• n == grid.length
• n == grid[r].length
• 2 <= n <= 50
• 0 <= grid[r][c] <= 100
Output: Return the maximum sum that the heights of the buildings can be increased by without affecting the skyline.
Example: Output: 10
Constraints:
• The sum must be calculated based on the heights of the buildings without affecting the skyline from any direction.
Goal: To maximize the height of buildings without altering the skyline, we need to calculate the maximum possible height increase for each building by comparing it to the skyline views from each direction.
Steps:
• Find the maximum building heights for each row and column (north-south and west-east).
• For each building, calculate the maximum height it can reach based on the minimum of the corresponding row and column maximums.
• Sum up the differences between the calculated maximum heights and the original building heights to get the total increase in height.
Goal: Ensure that the grid meets the problem constraints for the grid size and values.
Steps:
• n is between 2 and 50.
• Each building height is between 0 and 100.
Assumptions:
• The height of any building can be increased, but the changes must respect the skyline constraints.
Input: Input: grid = [[1, 3, 2],[4, 1, 5],[6, 3, 4]]
Explanation: In this grid, the maximum sum that the heights of buildings can be increased by is 10. The calculated skyline views from all four directions (north, south, east, and west) are taken into account to ensure that increasing the height of any building does not alter the skyline.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus