Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Complicated grid solution in Clear category for Building Visibility by kkkkk
def checkio(buildings):
"""Return number of buildings visible when viewed from the south.
Each building in the list contains the following information:
0: x_south_west,
1: y_south_west,
2: x_north_east,
3: y_north_east,
4: height
"""
# 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
# footprints.
#
# Each element in the grid will contain the building number and the
# height, with both values initialized to 0.
max_x = max(b[2] for b in buildings)
max_y = max(b[3] 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[0], building[2]):
for y in range(building[1], building[3]):
height_grid[y][x] = (building_num, building[4])
# 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][1] > last_height:
last_height = height_grid[y][x][1]
visible_buildings.add(height_grid[y][x][0])
return len(visible_buildings)
July 12, 2020