In the two-dimensional array grid
, grid[i][j]
represents the height of a building located at a certain location. We are allowed to increase the height of any number (the number of different buildings may vary) of buildings. The height 0
is also considered a building.
Finally, the "skyline" observed from all four directions (i.e. top, bottom, left, and right) of the new array must be the same as the skyline of the original array. The skyline of a city is the outer outline formed by all the buildings when viewed from a distance. Please refer to the example below.
What is the maximum total amount by which the building heights can be increased?
The basic idea of the problem is to increase each point in the two-dimensional array to the smaller value between the maximum value in the row and the maximum value in the column of the original two-dimensional array. By doing this, we get an increment for each point, and the final result is the sum of all the increments. We first iterate through the two-dimensional array and obtain all the maximum values of rows and columns and store them separately. Then we iterate through the array again, calculating the increment, which is the smaller value between the maximum values of the row and the column minus the current value, and then we add it to the count
. After the loop ends, we return count
.