- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Max Increase to Keep City Skyline in Python

Suppose we have a 2-dimensional array called grid, where each value of grid[i][j] represents the height of a building located there. We can increase the height of any number of buildings, by any amount. Height 0 is considered to be a building as well. At the end, the "skyline" when viewed from all four directions of the grid, must be the same as the skyline of the original grid. Because a city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. So we have to find the maximum total sum that the height of the buildings can be increased.

So, if the input is like

3 | 0 | 8 | 4 |

2 | 4 | 5 | 7 |

9 | 2 | 3 | 6 |

0 | 3 | 1 | 0 |

then the output will be 35, this is because the skyline viewed from top or bottom is: [9, 4, 8, 7], the skyline viewed from left or right is: [8, 7, 9, 3], So the final matrix can be like −

8 | 4 | 8 | 7 |

7 | 4 | 7 | 7 |

9 | 4 | 8 | 7 |

3 | 3 | 3 | 3 |

To solve this, we will follow these steps −

max_row_wise := a new list

max_column_wise := a new list

counter := 0

for each i in grid, do

insert maximum of i at the end of max_row_wise

counter := counter + 1

counter := 0, i := 0, j := 0

temp_list := a new list

Do the following infinitely −

insert grid[i,j] into temp_list

i := i + 1

if j is same as size of grid[0] -1 and i>=len(grid), then

insert maximum of temp_list at the end of max_column_wise

come out from the loop

otherwise when i >= size of grid , then

i := 0, j := j + 1

insert maximum of temp_list at the end of max_column_wise

counter := counter + 1

temp_list:= a new list

top_bottom, left_right := max_row_wise,max_column_wise

i, j, value := 0,0,0

Do the following infinitely, do

temp := minimum of [top_bottom[i], left_right[j]]

j := j + 1

if j is same as column length of grid and i is same as row count of grid -1, then

come out from the loop

otherwise when j is same as size of grid column , then

i := i+1

j := 0

return value

## Example

Let us see the following implementation to get a better understanding −

class Solution: def maxIncreaseKeepingSkyline(self, grid): max_row_wise = [] max_column_wise = [] counter = 0 for i in grid: max_row_wise.append(max(i)) counter+=1 counter = 0 i = 0 j = 0 temp_list = [] while True: temp_list.append(grid[i][j]) i+=1 if j ==len(grid[0])-1 and i>=len(grid): max_column_wise.append(max(temp_list)) break elif i >= len(grid): i = 0 j = j + 1 max_column_wise.append(max(temp_list)) counter +=1 temp_list=[] top_bottom, left_right = max_row_wise,max_column_wise i, j, value = 0,0,0 while True: temp = min([top_bottom[i], left_right[j]]) value+= abs(grid[i][j] - temp) j+=1 if j == len(grid[0]) and i==len(grid)-1: break elif j == len(grid[0]): i = i+1 j = 0 return value ob = Solution() print(ob.maxIncreaseKeepingSkyline([[3,0,8,4],[2,4,5,7],[9,2,6,3],[0, 3,1,0]]))

## Input

[[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]

## Output

35

- Related Questions & Answers
- Total Distance to Visit City Blocks in Python
- Program to check we can visit any city from any city or not in Python
- max() and min() in Python
- Is Max Heap in Python?
- Can we keep Python modules in compiled format?
- Two City Scheduling in C++
- Find maximum distance between any city and station in Python
- Which city of India is called the city of dreams and why?
- How to increase twitter followers?
- Program to fill Min-max game tree in Python
- How to get the longitude and latitude of a city using Python?
- How to keep appending subdocuments in MongoDB?
- Find current weather of any city using OpenWeatherMap API in Python
- Use of min() and max() in Python
- How to hide ticks label in Python but keep the ticks in place?