Advertisement
imashutosh51

The skyline Problem

Oct 25th, 2022 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. /*
  2. Observation:Change is only possible at building start or end.
  3. so break the starting and ending point of building where height at starting point will be positive of height
  4. and at ending point negative height so that we can differentiate between starting and ending point.
  5. eg (2,9,13) -> (2,13) and (9,-13)
  6. sort the above list because answer asked in question is in sorted order.
  7.  
  8. Let's say building present at current point is active buildings so,outline is maximum height among active buildings.
  9. so we maintain a max_heap which will maintain the maximum height building at top and that building is active or not,
  10. that can be taken care by map.
  11. so whenever we see positive height,means start of building then
  12.    if it's abs(height)>cur_height push it into answer,active building and map and also update the current_height.
  13.     if it's abs(height)<=cur_height push it into active building and map
  14. if current height is negative means end of building:
  15.    if it's height is same as cur_height,then remove that height from map,and find the maximum height among the
  16.     active buildings and update the cur_height if cur_height is different and update the answer else keep it same
  17.     Test Case: [[0,2,3],[2,5,3]]
  18.    
  19.     else remove from map.
  20.  
  21. One more example is : [0,2],[2,-2],[2,2],[4,-2] ans : [0,2],[4,0]
  22. here first building is ending at point 2 and second building is starting again at 2 so while sorting we will add first so my_fun is created otherwise answer in our above example will be at 2 is [2,0] then again go to [2,2].
  23.  
  24.  
  25. from heapq import heapify,heappush,heappop
  26. def my_fun(a):
  27.     return a[0],-a[1]
  28. class Solution:
  29.     def getSkyline(self, buil: List[List[int]]) -> List[List[int]]:
  30.         arr=[]
  31.         dict={}
  32.         heap=[]
  33.         heapify(heap)
  34.         for i in buil:
  35.             arr.append([i[0],i[2]])
  36.             arr.append([i[1],-i[2]])
  37.         arr.sort(key=my_fun)
  38.         cur_height=0
  39.         ans=[]
  40.         for i in arr:
  41.             if i[1]>0:  #means start of the building
  42.                 if i[1]>cur_height:
  43.                     ans.append([i[0],i[1]])
  44.                     cur_height=i[1]
  45.                     heappush(heap,-1*i[1])
  46.                     if i[1] in dict.keys():
  47.                         dict[i[1]]+=1
  48.                     else:
  49.                         dict[i[1]]=1
  50.                 elif i[1]<=cur_height:
  51.                     heappush(heap,-i[1])
  52.                     if i[1] in dict.keys():
  53.                         dict[i[1]]+=1
  54.                     else:
  55.                         dict[i[1]]=1
  56.             else:
  57.                 dict[abs(i[1])]-=1
  58.                 while len(heap)>0 and dict[abs(heap[0])]<=0:
  59.                     heappop(heap)
  60.                 if len(heap)>0:
  61.                     cur=abs(heap[0])
  62.                 else:
  63.                     cur=0
  64.                 if cur!=cur_height:
  65.                     ans.append([i[0],cur])
  66.                     cur_height=cur
  67.         return ans
  68.  
  69.  
  70.  
  71.  
  72.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement