5741:最高建筑高度
This commit is contained in:
parent
737d31db25
commit
4ed9262735
@ -34,48 +34,50 @@ import com.code.leet.entiy.TreeNode;
|
|||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
//897:递增顺序搜索树
|
//897:递增顺序搜索树
|
||||||
public class IncreasingOrderSearchTree{
|
public class IncreasingOrderSearchTree {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//测试代码
|
//测试代码
|
||||||
Solution solution = new IncreasingOrderSearchTree().new Solution();
|
Solution solution = new IncreasingOrderSearchTree().new Solution();
|
||||||
}
|
}
|
||||||
//力扣代码
|
//力扣代码
|
||||||
//leetcode submit region begin(Prohibit modification and deletion)
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
/**
|
|
||||||
* Definition for a binary tree node.
|
|
||||||
* public class TreeNode {
|
|
||||||
* int val;
|
|
||||||
* TreeNode left;
|
|
||||||
* TreeNode right;
|
|
||||||
* TreeNode() {}
|
|
||||||
* TreeNode(int val) { this.val = val; }
|
|
||||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
|
||||||
* this.val = val;
|
|
||||||
* this.left = left;
|
|
||||||
* this.right = right;
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
class Solution {
|
|
||||||
private TreeNode temp;
|
|
||||||
public TreeNode increasingBST(TreeNode root) {
|
|
||||||
TreeNode treeNode = new TreeNode(0);
|
|
||||||
temp = treeNode;
|
|
||||||
bst(root);
|
|
||||||
return treeNode.right;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void bst(TreeNode node){
|
/**
|
||||||
if (node == null) {
|
* Definition for a binary tree node.
|
||||||
return ;
|
* public class TreeNode {
|
||||||
|
* int val;
|
||||||
|
* TreeNode left;
|
||||||
|
* TreeNode right;
|
||||||
|
* TreeNode() {}
|
||||||
|
* TreeNode(int val) { this.val = val; }
|
||||||
|
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||||
|
* this.val = val;
|
||||||
|
* this.left = left;
|
||||||
|
* this.right = right;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
private TreeNode temp;
|
||||||
|
|
||||||
|
public TreeNode increasingBST(TreeNode root) {
|
||||||
|
TreeNode treeNode = new TreeNode(0);
|
||||||
|
temp = treeNode;
|
||||||
|
bst(root);
|
||||||
|
return treeNode.right;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bst(TreeNode node) {
|
||||||
|
if (node == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bst(node.left);
|
||||||
|
temp.right = node;
|
||||||
|
node.left = null;
|
||||||
|
temp = node;
|
||||||
|
bst(node.right);
|
||||||
}
|
}
|
||||||
bst(node.left);
|
|
||||||
temp.right = node;
|
|
||||||
node.left=null;
|
|
||||||
temp = node;
|
|
||||||
bst(node.right);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
//leetcode submit region end(Prohibit modification and deletion)
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,109 @@
|
|||||||
|
//在一座城市里,你需要建 n 栋新的建筑。这些新的建筑会从 1 到 n 编号排成一列。
|
||||||
|
//
|
||||||
|
// 这座城市对这些新建筑有一些规定:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 每栋建筑的高度必须是一个非负整数。
|
||||||
|
// 第一栋建筑的高度 必须 是 0 。
|
||||||
|
// 任意两栋相邻建筑的高度差 不能超过 1 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 除此以外,某些建筑还有额外的最高高度限制。这些限制会以二维整数数组 restrictions 的形式给出,其中 restrictions[i] = [id
|
||||||
|
//i, maxHeighti] ,表示建筑 idi 的高度 不能超过 maxHeighti 。
|
||||||
|
//
|
||||||
|
// 题目保证每栋建筑在 restrictions 中 至多出现一次 ,同时建筑 1 不会 出现在 restrictions 中。
|
||||||
|
//
|
||||||
|
// 请你返回 最高 建筑能达到的 最高高度 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 5, restrictions = [[2,1],[4,1]]
|
||||||
|
//输出:2
|
||||||
|
//解释:上图中的绿色区域为每栋建筑被允许的最高高度。
|
||||||
|
//我们可以使建筑高度分别为 [0,1,2,1,2] ,最高建筑的高度为 2 。
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 6, restrictions = []
|
||||||
|
//输出:5
|
||||||
|
//解释:上图中的绿色区域为每栋建筑被允许的最高高度。
|
||||||
|
//我们可以使建筑高度分别为 [0,1,2,3,4,5] ,最高建筑的高度为 5 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]
|
||||||
|
//输出:5
|
||||||
|
//解释:上图中的绿色区域为每栋建筑被允许的最高高度。
|
||||||
|
//我们可以使建筑高度分别为 [0,1,2,3,3,4,4,5,4,3] ,最高建筑的高度为 5 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 2 <= n <= 109
|
||||||
|
// 0 <= restrictions.length <= min(n - 1, 105)
|
||||||
|
// 2 <= idi <= n
|
||||||
|
// idi 是 唯一的 。
|
||||||
|
// 0 <= maxHeighti <= 109
|
||||||
|
//
|
||||||
|
// Related Topics 贪心算法 二分查找
|
||||||
|
// 👍 3 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
//5741:最高建筑高度
|
||||||
|
public class MaximumBuildingHeight {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new MaximumBuildingHeight().new Solution();
|
||||||
|
//2
|
||||||
|
System.out.println(solution.maxBuilding(5, new int[][]{{2, 1}, {4, 1}}));
|
||||||
|
//5
|
||||||
|
System.out.println(solution.maxBuilding(6, new int[][]{}));
|
||||||
|
//5
|
||||||
|
System.out.println(solution.maxBuilding(10, new int[][]{{5, 3}, {2, 5}, {7, 4}, {10, 3}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int maxBuilding(int n, int[][] restrictions) {
|
||||||
|
Arrays.sort(restrictions, (a, b) -> a[0] - b[0]);
|
||||||
|
|
||||||
|
for (int i = restrictions.length - 1; i > 0; i--) {
|
||||||
|
restrictions[i - 1][1] = Math.min(restrictions[i - 1][1],
|
||||||
|
restrictions[i][1] + restrictions[i][0] - restrictions[i - 1][0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int max = 0;
|
||||||
|
int last = 0;
|
||||||
|
int id = 1;
|
||||||
|
for (int[] restriction : restrictions){
|
||||||
|
int nextid = restriction[0];
|
||||||
|
int limit = restriction[1];
|
||||||
|
limit = Math.min(limit, nextid - id + last);
|
||||||
|
|
||||||
|
max = Math.max(max, (last + limit + nextid - id)/2);
|
||||||
|
|
||||||
|
last = limit;
|
||||||
|
id = nextid;
|
||||||
|
}
|
||||||
|
if (id < n){
|
||||||
|
max = Math.max(max, (n + Math.min(n, n - id + last) + last - id)/2);
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
<p>在一座城市里,你需要建 <code>n</code> 栋新的建筑。这些新的建筑会从 <code>1</code> 到 <code>n</code> 编号排成一列。</p>
|
||||||
|
|
||||||
|
<p>这座城市对这些新建筑有一些规定:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>每栋建筑的高度必须是一个非负整数。</li>
|
||||||
|
<li>第一栋建筑的高度 <strong>必须</strong> 是 <code>0</code> 。</li>
|
||||||
|
<li>任意两栋相邻建筑的高度差 <strong>不能超过</strong> <code>1</code> 。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>除此以外,某些建筑还有额外的最高高度限制。这些限制会以二维整数数组 <code>restrictions</code> 的形式给出,其中 <code>restrictions[i] = [id<sub>i</sub>, maxHeight<sub>i</sub>]</code> ,表示建筑 <code>id<sub>i</sub></code> 的高度 <strong>不能超过</strong> <code>maxHeight<sub>i</sub></code> 。</p>
|
||||||
|
|
||||||
|
<p>题目保证每栋建筑在 <code>restrictions</code> 中<strong> 至多出现一次</strong> ,同时建筑 <code>1</code> <strong>不会</strong> 出现在 <code>restrictions</code> 中。</p>
|
||||||
|
|
||||||
|
<p>请你返回 <strong>最高</strong> 建筑能达到的 <strong>最高高度</strong> 。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/04/25/ic236-q4-ex1-1.png" style="width: 400px; height: 253px;" />
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>n = 5, restrictions = [[2,1],[4,1]]
|
||||||
|
<b>输出:</b>2
|
||||||
|
<b>解释:</b>上图中的绿色区域为每栋建筑被允许的最高高度。
|
||||||
|
我们可以使建筑高度分别为 [0,1,2,1,2] ,最高建筑的高度为 2 。</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/04/25/ic236-q4-ex2.png" style="width: 500px; height: 269px;" />
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>n = 6, restrictions = []
|
||||||
|
<b>输出:</b>5
|
||||||
|
<b>解释:</b>上图中的绿色区域为每栋建筑被允许的最高高度。
|
||||||
|
我们可以使建筑高度分别为 [0,1,2,3,4,5] ,最高建筑的高度为 5 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/04/25/ic236-q4-ex3.png" style="width: 500px; height: 187px;" />
|
||||||
|
<pre>
|
||||||
|
<b>输入:</b>n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]
|
||||||
|
<b>输出:</b>5
|
||||||
|
<b>解释:</b>上图中的绿色区域为每栋建筑被允许的最高高度。
|
||||||
|
我们可以使建筑高度分别为 [0,1,2,3,3,4,4,5,4,3] ,最高建筑的高度为 5 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= n <= 10<sup>9</sup></code></li>
|
||||||
|
<li><code>0 <= restrictions.length <= min(n - 1, 10<sup>5</sup>)</code></li>
|
||||||
|
<li><code>2 <= id<sub>i</sub> <= n</code></li>
|
||||||
|
<li><code>id<sub>i</sub></code> 是 <strong>唯一的</strong> 。</li>
|
||||||
|
<li><code>0 <= maxHeight<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>贪心算法</li><li>二分查找</li></div></div>\n<div><li>👍 3</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user