返回介绍

solution / 2000-2099 / 2015.Average Height of Buildings in Each Segment / README_EN

发布于 2024-06-17 01:03:12 字数 9189 浏览 0 评论 0 收藏 0

2015. Average Height of Buildings in Each Segment

中文文档

Description

A perfectly straight street is represented by a number line. The street has building(s) on it and is represented by a 2D integer array buildings, where buildings[i] = [starti, endi, heighti]. This means that there is a building with heighti in the half-closed segment [starti, endi).

You want to describe the heights of the buildings on the street with the minimum number of non-overlapping segments. The street can be represented by the 2D integer array street where street[j] = [leftj, rightj, averagej] describes a half-closed segment [leftj, rightj) of the road where the average heights of the buildings in the segment is averagej.

  • For example, if buildings = [[1,5,2],[3,10,4]], the street could be represented by street = [[1,3,2],[3,5,3],[5,10,4]] because:
    • From 1 to 3, there is only the first building with an average height of 2 / 1 = 2.
    • From 3 to 5, both the first and the second building are there with an average height of (2+4) / 2 = 3.
    • From 5 to 10, there is only the second building with an average height of 4 / 1 = 4.

Given buildings, return _the 2D integer array _street_ as described above (excluding any areas of the street where there are no buldings). You may return the array in any order_.

The average of n elements is the sum of the n elements divided (integer division) by n.

A half-closed segment [a, b) is the section of the number line between points a and b including point a and not including point b.

 

Example 1:

Input: buildings = [[1,4,2],[3,9,4]]
Output: [[1,3,2],[3,4,3],[4,9,4]]
Explanation:
From 1 to 3, there is only the first building with an average height of 2 / 1 = 2.
From 3 to 4, both the first and the second building are there with an average height of (2+4) / 2 = 3.
From 4 to 9, there is only the second building with an average height of 4 / 1 = 4.

Example 2:

Input: buildings = [[1,3,2],[2,5,3],[2,8,3]]
Output: [[1,3,2],[3,8,3]]
Explanation:
From 1 to 2, there is only the first building with an average height of 2 / 1 = 2.
From 2 to 3, all three buildings are there with an average height of (2+3+3) / 3 = 2.
From 3 to 5, both the second and the third building are there with an average height of (3+3) / 2 = 3.
From 5 to 8, there is only the last building with an average height of 3 / 1 = 3.
The average height from 1 to 3 is the same so we can group them into one segment.
The average height from 3 to 8 is the same so we can group them into one segment.

Example 3:

Input: buildings = [[1,2,1],[5,6,1]]
Output: [[1,2,1],[5,6,1]]
Explanation:
From 1 to 2, there is only the first building with an average height of 1 / 1 = 1.
From 2 to 5, there are no buildings, so it is not included in the output.
From 5 to 6, there is only the second building with an average height of 1 / 1 = 1.
We cannot group the segments together because an empty space with no buildings seperates the segments.

 

Constraints:

  • 1 <= buildings.length <= 105
  • buildings[i].length == 3
  • 0 <= starti < endi <= 108
  • 1 <= heighti <= 105

Solutions

Solution 1: Differential Ordered Hash Table

We use the differential idea and an ordered hash table height to record the height change at each position, and cnt to record the change in the number of buildings. By calculating the prefix sum of the ordered hash table, we can get the height and the number of buildings at each position.

Finally, we traverse the ordered hash table. For each position, if both the height and the number of buildings are not zero, it means that there is a building at this position. We then check whether the average height of the building at this time is the same as that of the previous building. If it is the same, we merge them; otherwise, we add it to the result set.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of buildings.

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
    ans = []
    i = h = n = 0
    for j in sorted(cnt.keys()):
      if n:
        t = [i, j, h // n]
        if ans and ans[-1][1] == i and ans[-1][2] == t[-1]:
          ans[-1][1] = j
        else:
          ans.append(t)
      i = j
      h += height[j]
      n += cnt[j]
    return ans
class Solution {
  public int[][] averageHeightOfBuildings(int[][] buildings) {
    TreeMap<Integer, Integer> height = new TreeMap<>();
    TreeMap<Integer, Integer> cnt = new TreeMap<>();
    for (var v : buildings) {
      int s = v[0], e = v[1], h = v[2];
      cnt.put(s, cnt.getOrDefault(s, 0) + 1);
      cnt.put(e, cnt.getOrDefault(e, 0) - 1);
      height.put(s, height.getOrDefault(s, 0) + h);
      height.put(e, height.getOrDefault(e, 0) - h);
    }
    int i = 0, h = 0, n = 0;
    List<int[]> res = new ArrayList<>();
    for (int j : cnt.keySet()) {
      if (n > 0) {
        int[] t = new int[] {i, j, h / n};
        int k = res.size() - 1;
        if (k >= 0 && res.get(k)[1] == i && res.get(k)[2] == t[2]) {
          res.get(k)[1] = j;
        } else {
          res.add(t);
        }
      }
      h += height.get(j);
      n += cnt.get(j);
      i = j;
    }
    int[][] ans = new int[res.size()][3];
    for (i = 0; i < ans.length; ++i) {
      ans[i] = res.get(i);
    }
    return ans;
  }
}
class Solution {
public:
  vector<vector<int>> averageHeightOfBuildings(vector<vector<int>>& buildings) {
    map<int, int> height, cnt;
    for (auto& v : buildings) {
      int s = v[0], e = v[1], h = v[2];
      cnt[s]++, cnt[e]--;
      height[s] += h, height[e] -= h;
    }
    vector<vector<int>> ans;
    int i = 0, h = 0, n = 0;
    for (auto& [j, _] : cnt) {
      if (n) {
        vector<int> t = {i, j, h / n};
        if (ans.size() && ans.back()[1] == i && ans.back()[2] == t[2]) {
          ans.back()[1] = j;
        } else {
          ans.push_back(t);
        }
      }
      i = j;
      h += height[j];
      n += cnt[j];
    }
    return ans;
  }
};
func averageHeightOfBuildings(buildings [][]int) [][]int {
  height := make(map[int]int)
  cnt := make(map[int]int)
  for _, v := range buildings {
    s, e, h := v[0], v[1], v[2]
    cnt[s]++
    cnt[e]--
    height[s] += h
    height[e] -= h
  }
  keys := make([]int, len(cnt))
  for k := range cnt {
    keys = append(keys, k)
  }
  sort.Ints(keys)
  i, h, n := 0, 0, 0
  ans := [][]int{}
  for _, j := range keys {
    if n > 0 {
      t := []int{i, j, h / n}
      if len(ans) > 0 && ans[len(ans)-1][1] == i && ans[len(ans)-1][2] == t[2] {
        ans[len(ans)-1][1] = j
      } else {
        ans = append(ans, t)
      }
    }
    i = j
    h += height[j]
    n += cnt[j]
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文