The Skyline Problem

Solution Code
class Solution:
    def getSkyline(self, buildings: List[List[int]]) -> List[List[int]]:
        events = []
        for L, R, H in buildings:
            events.append((L, -H, R))
            events.append((R, 0, 0))
        
        events.sort()
        hp = [(0, float('inf'))]
        ans = []
        
        for x, neg_h, r in events:
            while hp[0][1] <= x:
                heapq.heappop(hp)
            if neg_h < 0:
                heapq.heappush(hp, (neg_h, r))
            
            curr_max = -hp[0][0]
            if not ans or ans[-1][1] != curr_max:
                ans.append([x, curr_max])
        return ans
Video GuideLeetcode Daily

Time Complexity

O(N log N)

Space Complexity

O(N)