Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Max Increase to Keep City Skyline in Python
The Max Increase to Keep City Skyline problem asks us to find the maximum total sum that building heights can be increased while maintaining the original skyline when viewed from all four directions. The skyline from each direction is determined by the maximum height in each row (left/right view) and each column (top/bottom view).
Problem Understanding
Given a 2D grid where each value represents a building height, we need to ?
- Calculate the skyline from left/right (maximum in each row)
- Calculate the skyline from top/bottom (maximum in each column)
- For each position, the maximum possible height is the minimum of its row max and column max
- Sum the differences between possible heights and current heights
Example
For the input grid ?
| 3 | 0 | 8 | 4 |
| 2 | 4 | 5 | 7 |
| 9 | 2 | 6 | 3 |
| 0 | 3 | 1 | 0 |
The row maximums are [8, 7, 9, 3] and column maximums are [9, 4, 8, 7].
Optimized Solution
def maxIncreaseKeepingSkyline(grid):
# Calculate row maximums (left/right skyline)
row_max = [max(row) for row in grid]
# Calculate column maximums (top/bottom skyline)
col_max = [max(grid[i][j] for i in range(len(grid))) for j in range(len(grid[0]))]
total_increase = 0
# Calculate maximum possible increase for each position
for i in range(len(grid)):
for j in range(len(grid[0])):
# Maximum height at position (i,j) is min of row max and column max
max_height = min(row_max[i], col_max[j])
total_increase += max_height - grid[i][j]
return total_increase
# Test with the example
grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
result = maxIncreaseKeepingSkyline(grid)
print(f"Maximum increase: {result}")
Maximum increase: 35
Step-by-Step Breakdown
grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
# Step 1: Calculate row maximums
row_max = [max(row) for row in grid]
print("Row maximums (left/right skyline):", row_max)
# Step 2: Calculate column maximums
col_max = [max(grid[i][j] for i in range(len(grid))) for j in range(len(grid[0]))]
print("Column maximums (top/bottom skyline):", col_max)
# Step 3: Show the calculation for each position
print("\nPosition-wise calculation:")
total = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
current = grid[i][j]
max_possible = min(row_max[i], col_max[j])
increase = max_possible - current
total += increase
print(f"({i},{j}): {current} ? {max_possible}, increase: {increase}")
print(f"\nTotal increase: {total}")
Row maximums (left/right skyline): [8, 7, 9, 3] Column maximums (top/bottom skyline): [9, 4, 8, 7] Position-wise calculation: (0,0): 3 ? 8, increase: 5 (0,1): 0 ? 4, increase: 4 (0,2): 8 ? 8, increase: 0 (0,3): 4 ? 7, increase: 3 (1,0): 2 ? 7, increase: 5 (1,1): 4 ? 4, increase: 0 (1,2): 5 ? 7, increase: 2 (1,3): 7 ? 7, increase: 0 (2,0): 9 ? 9, increase: 0 (2,1): 2 ? 4, increase: 2 (2,2): 6 ? 8, increase: 2 (2,3): 3 ? 7, increase: 4 (3,0): 0 ? 3, increase: 3 (3,1): 3 ? 3, increase: 0 (3,2): 1 ? 3, increase: 2 (3,3): 0 ? 3, increase: 3 Total increase: 35
Algorithm Complexity
The solution has O(m × n) time complexity where m and n are the grid dimensions. We traverse the grid three times: once for row maximums, once for column maximums, and once for calculating increases.
Conclusion
The key insight is that each building's maximum height is constrained by both its row maximum and column maximum. The solution efficiently calculates these constraints and sums the possible increases for all positions.
