You want to build n
new buildings in a city. The new buildings will be built in a line and are labeled from 1
to n
.
However, there are city restrictions on the heights of the new buildings:
- The height of each building must be a non-negative integer.
- The height of the first building must be
0
. - The height difference between any two adjacent buildings cannot exceed
1
.
Additionally, there are city restrictions on the maximum height of specific buildings. These restrictions are given as a 2D integer array restrictions
where restrictions[i] = [idi, maxHeighti]
indicates that building idi
must have a height less than or equal to maxHeighti
.
It is guaranteed that each building will appear at most once in restrictions
, and building 1
will not be in restrictions
.
Return the maximum possible height of the tallest building.
Example 1:
Input: n = 5, restrictions = [[2,1],[4,1]] Output: 2 Explanation: The green area in the image indicates the maximum allowed height for each building. We can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 2.
Example 2:
Input: n = 6, restrictions = [] Output: 5 Explanation: The green area in the image indicates the maximum allowed height for each building. We can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5.
Example 3:
Input: n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]] Output: 5 Explanation: The green area in the image indicates the maximum allowed height for each building. We can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a height of 5.
Constraints:
2 <= n <= 109
0 <= restrictions.length <= min(n - 1, 105)
2 <= idi <= n
idi
is unique.0 <= maxHeighti <= 109
最高建筑高度。
在一座城市里,你需要建 n 栋新的建筑。这些新的建筑会从 1 到 n 编号排成一列。
这座城市对这些新建筑有一些规定:
每栋建筑的高度必须是一个非负整数。
第一栋建筑的高度 必须 是 0 。
任意两栋相邻建筑的高度差 不能超过 1 。
除此以外,某些建筑还有额外的最高高度限制。这些限制会以二维整数数组 restrictions 的形式给出,其中 restrictions[i] = [idi, maxHeighti] ,表示建筑 idi 的高度 不能超过 maxHeighti 。题目保证每栋建筑在 restrictions 中 至多出现一次 ,同时建筑 1 不会 出现在 restrictions 中。
请你返回 最高 建筑能达到的 最高高度 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-building-height
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题是 hard。我参考了这个官方题解,写的很好。题目给的是一个一维数组,请你按照数组里给的 restrictions 的要求建造房子,并且房子的高度要按照 restrictions 来。思路和讲解我觉得可以直接参考官方题解,我这里直接贴出代码。
时间O(nlogn)
空间O(1)
Java实现
1 class Solution { 2 public int maxBuilding(int n, int[][] r) { 3 // corner case 4 if (r == null || r.length == 0) { 5 return n - 1; 6 } 7 8 // normal case 9 // Arrays.sort(r, Comparator.comparingInt(x -> x[0])); 10 Arrays.sort(r, (a, b) -> a[0] - b[0]); 11 int len = r.length; 12 for (int i = len - 2; i >= 0; i--) { 13 r[i][1] = Math.min(r[i][1], Math.min(r[i][0] - 1, r[i + 1][1] + r[i + 1][0] - r[i][0])); 14 } 15 16 int maxHeight = (0 + r[0][1] + r[0][0] - 1) / 2; 17 for (int i = 1; i <= len - 1; i++) { 18 r[i][1] = Math.min(r[i][1], Math.min(r[i][0] - 1, r[i - 1][1] + r[i][0] - r[i - 1][0])); 19 maxHeight = Math.max(maxHeight, (r[i - 1][1] + r[i][1] + r[i][0] - r[i - 1][0]) / 2); 20 } 21 maxHeight = Math.max(maxHeight, r[len - 1][1] + n - r[len - 1][0]); 22 return maxHeight; 23 } 24 }