
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
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
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]]
- Related Articles
- Program to find maximum building height in Python
- Python Program for Maximum height when coins are arranged in a triangle
- Program to find maximum XOR for each query in Python
- Write a Program to Find the Maximum Depth or Height of a Tree in C++
- Python program to find the redundancy rates for each row of a matrix
- JavaScript Program to Find maximum element of each row in a matrix
- C++ program to find maximum possible value for which XORed sum is maximum
- How to find the maximum value for each column of a matrix in R?
- Python Program to Read Height in Centimeters and convert the Height to Feet and Inches
- Program to find maximum non negative product in a matrix in Python
- Program to find maximum possible population of all the cities in python
- Program to find maximum possible value of smallest group in Python
- Program to find out the cells containing maximum value in a matrix in Python
- Find the maximum element of each row in a matrix using Python
- Program to find the maximum score from all possible valid paths in Python
