# Program to find a matrix for each condominium's height is increased to the maximum possible height in Python?

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

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.

So, if the input is like

 2 3 4 5 6 7 8 9 10

 4 4 4 7 7 7 8 9 10

as 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.

To solve this, we will follow these steps:

• r := a list of maximum of each row in the matrix

• c := a list of maximum of each column in the matrix

• for i in range 0 to row count of matrix, do

• for j in range 0 to column count of matrix, do

• if r[i] < c[j], then

• matrix[i, j] := r[i]

• otherwise,

• matrix[i, j] := c[j]

• return matrix

Let us see the following implementation to get better understanding:

## Example

Live Demo

class Solution:
def solve(self, matrix):
r = [max(i) for i in matrix]
c = [max(i) for i in zip(*matrix)]
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if r[i] < c[j]:
matrix[i][j] = r[i]
else:
matrix[i][j] = c[j]
return matrix

ob = Solution()
matrix = [
[2, 3, 4],
[5, 6, 7],
[8, 9, 10]
]
print(ob.solve(matrix))

## Input

[[2, 3, 4],
[5, 6, 7],
[8, 9, 10]]

## Output

[[4, 4, 4], [7, 7, 7], [8, 9, 10]]
Updated on 10-Nov-2020 08:44:10