Max Increase to Keep City Skyline in Python

PythonServer Side ProgrammingProgramming

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

3084
2457
9236
0310

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 −

8487
7477
9487
3333

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 −

 Live Demo

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
raja
Updated on 17-Nov-2020 11:09:19

Advertisements