My 15 lines Python solution.


  • 3
    Y

    The idea is the same with https://leetcode.com/discuss/37736/108-ms-17-lines-body-explained. Thanks, StefanPochmann!

        def getSkyline(self, buildings):
        events = sorted(buildings + [[b[1], float("inf"), 0] for b in buildings])
        skyline, curb = [], [(0, float("inf"))]
        for L, R, H in events:
            top = curb[0][0]
            while curb[0][1]<= L:
                heapq.heappop(curb)
            if H > 0: heapq.heappush(curb,(-H, R))
            if top != curb[0][0]:
                if skyline and L == skyline[-1][0]:
                    skyline[-1][1] = -curb[0][0]
                else:
                    skyline.append([L, -curb[0][0]])
        return skyline

  • 0

    Very nice. But try for L, R, H in events: (or for left, right, height in events:), for better readability (yeah, I should have done something like that in my solution as well, probably didn't because I was translating too closely from C++).


  • 0
    Y

    That is a nice suggestion, done!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.