coding utf8 a city s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance now suppose you are given the locations and height of all the buildings as shown on a cityscape photo figure a write a program to output the skyline formed by these buildings collectively figure b the geometric information of each building is represented by a triplet of integers li ri hi where li and ri are the x coordinates of the left and right edge of the ith building respectively and hi is its height it is guaranteed that 0 li ri intmax 0 hi intmax and ri li 0 you may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0 for instance the dimensions of all buildings in figure a are recorded as 2 9 10 3 7 15 5 12 12 15 20 10 19 24 8 the output is a list of key points red dots in figure b in the format of x1 y1 x2 y2 x3 y3 that uniquely defines a skyline a key point is the left endpoint of a horizontal line segment note that the last key point where the rightmost building ends is merely used to mark the termination of the skyline and always has zero height also the ground in between any two adjacent buildings should be considered part of the skyline contour for instance the skyline in figure b should be represented as 2 10 3 15 7 12 12 0 15 10 20 8 24 0 notes the number of buildings in any input list is guaranteed to be in the range 0 10000 the input list is already sorted in ascending order by the left x position li the output list must be sorted by the x position there must be no consecutive horizontal lines of equal height in the output skyline for instance 2 3 4 5 7 5 11 5 12 7 is not acceptable the three lines of height 5 should be merged into one in the final output as such 2 3 4 5 12 7 wortst time complexity onlogn type buildings listlistint rtype listlistint coding utf 8 a city s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance now suppose you are given the locations and height of all the buildings as shown on a cityscape photo figure a write a program to output the skyline formed by these buildings collectively figure b the geometric information of each building is represented by a triplet of integers li ri hi where li and ri are the x coordinates of the left and right edge of the ith building respectively and hi is its height it is guaranteed that 0 li ri int_max 0 hi int_max and ri li 0 you may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0 for instance the dimensions of all buildings in figure a are recorded as 2 9 10 3 7 15 5 12 12 15 20 10 19 24 8 the output is a list of key points red dots in figure b in the format of x1 y1 x2 y2 x3 y3 that uniquely defines a skyline a key point is the left endpoint of a horizontal line segment note that the last key point where the rightmost building ends is merely used to mark the termination of the skyline and always has zero height also the ground in between any two adjacent buildings should be considered part of the skyline contour for instance the skyline in figure b should be represented as 2 10 3 15 7 12 12 0 15 10 20 8 24 0 notes the number of buildings in any input list is guaranteed to be in the range 0 10000 the input list is already sorted in ascending order by the left x position li the output list must be sorted by the x position there must be no consecutive horizontal lines of equal height in the output skyline for instance 2 3 4 5 7 5 11 5 12 7 is not acceptable the three lines of height 5 should be merged into one in the final output as such 2 3 4 5 12 7 wortst time complexity o nlogn type buildings list list int rtype list list int
import heapq

def get_skyline(lrh):
    
    skyline, live = [], []
    i, n = 0, len(lrh)
    while i < n or live:
        if not live or i < n and lrh[i][0] <= -live[0][1]:
            x = lrh[i][0]
            while i < n and lrh[i][0] == x:
                heapq.heappush(live, (-lrh[i][2], -lrh[i][1]))
                i += 1
        else:
            x = -live[0][1]
            while live and -live[0][1] <= x:
                heapq.heappop(live)
        height = len(live) and -live[0][0]
        if not skyline or height != skyline[-1][1]:
            skyline += [x, height],
    return skyline