Complicated grid solution in Clear category for Building Visibility by kkkkk
"""Return number of buildings visible when viewed from the south.
Each building in the list contains the following information:
# Initialize a grid that will hold the combined footprint for all of the
# buildings. The grid will represent a birds eye view of the building
# Each element in the grid will contain the building number and the
# height, with both values initialized to 0.
max_x = max(b for b in buildings)
max_y = max(b for b in buildings)
height_grid = [[(0, 0)] * max_x for j in range(max_y)]
# Now store the actual height and building numbers for the buildings in
# the grid.
for building_num, building in enumerate(buildings):
for x in range(building, building):
for y in range(building, building):
height_grid[y][x] = (building_num, building)
# Walk down each column (y-value), ignoring heights that are less than
# the previous heights. Save the building numbers where the heights are
# not ignored.
visible_buildings = set()
for x in range(max_x):
last_height = 0
for y in range(max_y):
if height_grid[y][x] > last_height:
last_height = height_grid[y][x]
July 12, 2020