Skip to content

Instantly share code, notes, and snippets.

@felipernb
Created August 7, 2012 09:05
Show Gist options
  • Save felipernb/3283573 to your computer and use it in GitHub Desktop.
Save felipernb/3283573 to your computer and use it in GitHub Desktop.
The skyline problem - http://acm.uva.es/p/v1/105.html
#problem specification: http://acm.uva.es/p/v1/105.html
buildings = [(1,11,5), (2,6,7), (3,13,9), (12,7,16), (14,3,25), (19,18,22), (23,13,29), (24,4,28)]
def skyline(buildings):
#initialize the skyline, setting 0 in all points
line = [0 for i in xrange(max(b[2] for b in buildings)+1)]
for b in buildings:
for x in xrange(b[0],b[2]):
line[x] = max(line[x], b[1])
skyline = []
actual_height = 0
for (x, y) in enumerate(line):
if y != actual_height:
skyline.append((x,y))
actual_height = y
return skyline
skyline(buildings) #[(1, 11), (3, 13), (9, 0), (12, 7), (16, 3), (19, 18), (22, 3), (23, 13), (29, 0)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment