2015. Average Height of Buildings in Each Segment

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2015

class Solution:
    def averageHeightOfBuildings(self, buildings: List[List[int]]) -> List[List[int]]:
        height = defaultdict(int)
        cnt = defaultdict(int)
        for s, e, h in buildings:
            cnt[s] += 1
            cnt[e] -= 1
            height[s] += h
            height[e] -= h
        res = []
        i = h = n = 0
        for j in sorted(cnt.keys()):
            if n:
                t = [i, j, h // n]
                if res and res[-1][1] == i and res[-1][2] == t[-1]:
                    res[-1][1] = j
                else:
                    res.append(t)
            i = j
            h += height[j]
            n += cnt[j]
        return res