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
Program to find a matrix for each condominium's height is increased to the maximum possible height in Python?
Suppose we have a 2D matrix where matrix[r][c] represents the height of a condominium in a city. The west-east skyline is visible by taking the maximum of each row in the matrix, and the north-south skyline can be visible by taking the maximum of each column. We have to find a new matrix where each condominium's height is increased to the maximum possible height while keeping the same west-east and north-south skyline.
Understanding the Problem
Given a matrix like this:
| 2 | 3 | 4 |
| 5 | 6 | 7 |
| 8 | 9 | 10 |
The expected output is:
| 4 | 4 | 4 |
| 7 | 7 | 7 |
| 8 | 9 | 10 |
The west-east skyline is [4, 7, 10] and north-south skyline is [8, 9, 10]. We can increase everything in the first row to value 4 and everything in the second row to value 7 without changing the skylines.
Algorithm
To solve this problem, we will follow these steps:
row_max:= a list of maximum of each row in the matrixcol_max:= a list of maximum of each column in the matrixFor each position (i, j), set
matrix[i][j] = min(row_max[i], col_max[j])Return the modified matrix
Implementation
class Solution:
def solve(self, matrix):
# Calculate maximum of each row
row_max = [max(row) for row in matrix]
# Calculate maximum of each column using zip
col_max = [max(col) for col in zip(*matrix)]
# Update each cell to minimum of its row and column maximum
for i in range(len(matrix)):
for j in range(len(matrix[i])):
matrix[i][j] = min(row_max[i], col_max[j])
return matrix
# Test the solution
ob = Solution()
matrix = [
[2, 3, 4],
[5, 6, 7],
[8, 9, 10]
]
print("Original matrix:")
for row in matrix:
print(row)
result = ob.solve(matrix)
print("\nResult matrix:")
for row in result:
print(row)
Original matrix: [2, 3, 4] [5, 6, 7] [8, 9, 10] Result matrix: [4, 4, 4] [7, 7, 7] [8, 9, 10]
How It Works
The key insight is that for each position (i, j), the maximum possible height is constrained by both the row maximum and column maximum. The height cannot exceed either constraint, so we take the minimum of both values.
For the given example:
Row maximums: [4, 7, 10]
Column maximums: [8, 9, 10]
Position (0,0): min(4, 8) = 4
Position (0,1): min(4, 9) = 4
Position (2,2): min(10, 10) = 10
Alternative Implementation
Here's a more concise version using list comprehension:
def maximize_heights(matrix):
row_max = [max(row) for row in matrix]
col_max = [max(col) for col in zip(*matrix)]
return [[min(row_max[i], col_max[j])
for j in range(len(matrix[0]))]
for i in range(len(matrix))]
# Test the function
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
result = maximize_heights(matrix)
for row in result:
print(row)
[3, 3, 3] [6, 6, 6] [7, 8, 9]
Conclusion
The solution works by setting each cell to the minimum of its row maximum and column maximum. This ensures that the west-east and north-south skylines remain unchanged while maximizing each condominium's height.
