|
| 1 | +# [2015. Average Height of Buildings in Each Segment](https://leetcode-cn.com/problems/average-height-of-buildings-in-each-segment) |
| 2 | + |
| 3 | +[English Version](/solution/2000-2099/2015.Average%20Height%20of%20Buildings%20in%20Each%20Segment/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>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 <code>buildings</code>, where <code>buildings[i] = [start<sub>i</sub>, end<sub>i</sub>, height<sub>i</sub>]</code>. This means that there is a building with <code>height<sub>i</sub></code> in the <strong>half-closed segment</strong> <code>[start<sub>i</sub>, end<sub>i</sub>)</code>.</p> |
| 10 | + |
| 11 | +<p>You want to <strong>describe</strong> the heights of the buildings on the street with the <strong>minimum</strong> number of non-overlapping <strong>segments</strong>. The street can be represented by the 2D integer array <code>street</code> where <code>street[j] = [left<sub>j</sub>, right<sub>j</sub>, average<sub>j</sub>]</code> describes a <strong>half-closed segment</strong> <code>[left<sub>j</sub>, right<sub>j</sub>)</code> of the road where the <strong>average</strong> heights of the buildings in the<strong> segment</strong> is <code>average<sub>j</sub></code>.</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>For example, if <code>buildings = [[1,5,2],[3,10,4]],</code> the street could be represented by <code>street = [[1,3,2],[3,5,3],[5,10,4]]</code> because: |
| 15 | + <ul> |
| 16 | + <li>From 1 to 3, there is only the first building with an average height of <code>2 / 1 = 2</code>.</li> |
| 17 | + <li>From 3 to 5, both the first and the second building are there with an average height of <code>(2+4) / 2 = 3</code>.</li> |
| 18 | + <li>From 5 to 10, there is only the second building with an average height of <code>4 / 1 = 4</code>.</li> |
| 19 | + </ul> |
| 20 | + </li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>Given <code>buildings</code>, return <em>the 2D integer array </em><code>street</code><em> as described above (<strong>excluding</strong> any areas of the street where there are no buldings). You may return the array in <strong>any order</strong></em>.</p> |
| 24 | + |
| 25 | +<p>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</p> |
| 26 | + |
| 27 | +<p>A <strong>half-closed segment</strong> <code>[a, b)</code> is the section of the number line between points <code>a</code> and <code>b</code> <strong>including</strong> point <code>a</code> and <strong>not including</strong> point <code>b</code>.</p> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | +<p><strong>Example 1:</strong></p> |
| 31 | +<img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/2000-2099/2015.Average%20Height%20of%20Buildings%20in%20Each%20Segment/images/image-20210921224001-2.png" style="width: 500px; height: 349px;" /> |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> buildings = [[1,5,2],[3,10,4]] |
| 34 | +<strong>Output:</strong> [[1,3,2],[3,5,3],[5,10,4]] |
| 35 | +<strong>Explanation:</strong> |
| 36 | +From 1 to 3, there is only the first building with an average height of 2 / 1 = 2. |
| 37 | +From 3 to 5, both the first and the second building are there with an average height of (2+4) / 2 = 3. |
| 38 | +From 5 to 10, there is only the second building with an average height of 4 / 1 = 4. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><strong>Example 2:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> buildings = [[1,3,2],[2,5,3],[2,8,3]] |
| 45 | +<strong>Output:</strong> [[1,3,2],[3,8,3]] |
| 46 | +<strong>Explanation:</strong> |
| 47 | +From 1 to 2, there is only the first building with an average height of 2 / 1 = 2. |
| 48 | +From 2 to 3, all three buildings are there with an average height of (2+3+3) / 3 = 2. |
| 49 | +From 3 to 5, both the second and the third building are there with an average height of (3+3) / 2 = 3. |
| 50 | +From 5 to 8, there is only the last building with an average height of 3 / 1 = 3. |
| 51 | +The average height from 1 to 3 is the same so we can group them into one segment. |
| 52 | +The average height from 3 to 8 is the same so we can group them into one segment. |
| 53 | +</pre> |
| 54 | + |
| 55 | +<p><strong>Example 3:</strong></p> |
| 56 | + |
| 57 | +<pre> |
| 58 | +<strong>Input:</strong> buildings = [[1,2,1],[5,6,1]] |
| 59 | +<strong>Output:</strong> [[1,2,1],[5,6,1]] |
| 60 | +<strong>Explanation:</strong> |
| 61 | +From 1 to 2, there is only the first building with an average height of 1 / 1 = 1. |
| 62 | +From 2 to 5, there are no buildings, so it is not included in the output. |
| 63 | +From 5 to 6, there is only the second building with an average height of 1 / 1 = 1. |
| 64 | +We cannot group the segments together because an empty space with no buildings seperates the segments. |
| 65 | +</pre> |
| 66 | + |
| 67 | +<p> </p> |
| 68 | +<p><strong>Constraints:</strong></p> |
| 69 | + |
| 70 | +<ul> |
| 71 | + <li><code>1 <= buildings.length <= 10<sup>5</sup></code></li> |
| 72 | + <li><code>buildings[i].length == 3</code></li> |
| 73 | + <li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 10<sup>8</sup></code></li> |
| 74 | + <li><code>1 <= height<sub>i</sub> <= 10<sup>5</sup></code></li> |
| 75 | +</ul> |
| 76 | + |
| 77 | + |
| 78 | +## 解法 |
| 79 | + |
| 80 | +<!-- 这里可写通用的实现逻辑 --> |
| 81 | + |
| 82 | +<!-- tabs:start --> |
| 83 | + |
| 84 | +### **Python3** |
| 85 | + |
| 86 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 87 | + |
| 88 | +```python |
| 89 | + |
| 90 | +``` |
| 91 | + |
| 92 | +### **Java** |
| 93 | + |
| 94 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 95 | + |
| 96 | +```java |
| 97 | + |
| 98 | +``` |
| 99 | + |
| 100 | +### **...** |
| 101 | + |
| 102 | +``` |
| 103 | +
|
| 104 | +``` |
| 105 | + |
| 106 | +<!-- tabs:end --> |
0 commit comments