Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. class Solution:
  2. # @param {integer[][]} buildings
  3. # @return {integer[][]}
  4. def getSkyline(self, buildings):
  5. if buildings==[]:
  6. return []
  7. if len(buildings)==1:
  8. return [[buildings[0][0],buildings[0][2]],[buildings[0][1],0]]
  9. mid=(len(buildings)-1)/2
  10. left=self.getSkyline(buildings[0:mid+1])
  11. right=self.getSkyline(buildings[mid+1:])
  12. return self.merge(left,right)
  13. def merge(self,left,right):
  14. i=0
  15. j=0
  16. result=[]
  17. h1=None
  18. h2=None
  19. while i<len(left) and j<len(right):
  20. if left[i][0]<right[j][0]:
  21. h1=left[i][1]
  22. new=[left[i][0],max(h1,h2)]
  23. if result==[] or result[-1][1]!=new[1]:
  24. result.append(new)
  25. i+=1
  26. elif left[i][0]>right[j][0]:
  27. h2=right[j][1]
  28. new=[right[j][0],max(h1,h2)]
  29. if result==[] or result[-1][1]!=new[1]:
  30. result.append(new)
  31. j+=1
  32. else:
  33. h1=left[i][1]
  34. h2=right[j][1]
  35. new=[right[j][0],max(h1,h2)]
  36. if result==[] or result[-1][1]!=new[1]:
  37. result.append([right[j][0],max(h1,h2)])
  38. i+=1
  39. j+=1
  40. while i<len(left):
  41. if result==[] or result[-1][1]!=left[i][1]:
  42. result.append(left[i][:])
  43. i+=1
  44. while j<len(right):
  45. if result==[] or result[-1][1]!=right[j][1]:
  46. result.append(right[j][:])
  47. j+=1
  48.  
  49. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement