Advertisement
Guest User

Skyline Problem Solution

a guest
Feb 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. class Solution:
  2.     def getSkyline(self, buildings):
  3.         """
  4.        :type buildings: List[List[int]]
  5.        :rtype: List[List[int]]
  6.        """
  7.         vectors = {}
  8.         for li, ri, hi in buildings:
  9.             vectors[li] = ([], [])
  10.             vectors[ri] = ([], [])
  11.  
  12.         for li, ri, hi in buildings:
  13.             vectors[li][0].append(hi)
  14.             vectors[ri][1].append(hi)
  15.  
  16.         fringe = [0]
  17.         skyline = []
  18.         last_height = 0
  19.         for x, (ups, downs) in sorted(vectors.items()):
  20.             fringe.extend(ups)
  21.             for e in downs:
  22.                 fringe.remove(e)
  23.  
  24.             height = max(fringe)
  25.             if height != last_height:
  26.                 skyline.append((x, height))
  27.                 last_height = height
  28.         return skyline
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement