Java for LeetCode 218 The Skyline Problem【HARD】
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
解题思路:
本题如果一个矩形一个矩形的加入,涉及到回溯问题,比较复杂。
一个简便的思路是,根据横坐标排序,然后遍历求拐点。求拐点的时候用一个最大化heap来保存当前的楼顶高度,遇到左边节点,就在heap中插入高度信息,遇到右边节点就 从heap中删除高度。分别用pre与cur来表示之前的高度与当前的高度,当cur != pre的时候说明出现了拐点。JAVA实现如下:
static public List<int[]> getSkyline(int[][] buildings) {
List<int[]> res = new ArrayList<int[]>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(11,
new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b - a;
}
});
List<int[]> bl = new ArrayList<int[]>();
for (int i = 0; i < buildings.length; i++) {
bl.add(new int[] { buildings[i][0], buildings[i][2] });
bl.add(new int[] { buildings[i][1], -buildings[i][2] });
}
Collections.sort(bl, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] == b[0] ? b[1] - a[1] : a[0] - b[0];
}
});
int pre = 0, cur = 0;
for (int i = 0; i < bl.size(); i++) {
int[] b = bl.get(i);
if (b[1] > 0) {
maxHeap.add(b[1]);
cur = maxHeap.peek();
} else {
maxHeap.remove(-b[1]);
cur = (maxHeap.peek() == null) ? 0 : maxHeap.peek();
}
if (cur != pre) {
res.add(new int[] { b[0], cur });
pre = cur;
}
}
return res;
}
Java for LeetCode 218 The Skyline Problem【HARD】的更多相关文章
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode 218. The Skyline Problem 天际线问题(C++/Java)
题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...
- [LeetCode] 218. The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [LeetCode#218] The Skyline Problem
Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...
- LeetCode:组合总数III【216】
LeetCode:组合总数III[216] 题目描述 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. ...
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:括号的分数【856】
LeetCode:括号的分数[856] 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- LeetCode:整数转罗马数字【12】
LeetCode:整数转罗马数字[12] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...
随机推荐
- 新浪微博客户端(34)-block的细节与本质
main.m #import <Foundation/Foundation.h> void test4(); int main(int argc, const char * argv[]) ...
- shell学习之路:shell基础大全2
原文:http://note.youdao.com/share/?id=cd2ad6e6d5db2b347f92958efc2bdbc1&type=note 正则表达式与通配符: 一.介绍: ...
- 遍历一个类的属性--并转换为Dictionary类型
参考地址...http://www.cnblogs.com/xwgli/p/3306297.html 记录点滴...以前很少用泛型...HaHa... /// <summary> /// ...
- jquery 使用textarea
问题: 若在textarea标签中键入一个回车时,将插入2个字符\n\r ,将在datagrid不能正确显示. \n: 回车符 \r: 换行符 解决方案: txt = txt.Replace(&qu ...
- Hanoi问题
#include<stdio.h>int main(){ int m; void hanoi(int n,char x,char y,char z); printf("input ...
- 使一个div始终显示在页面中间
使一个div始终显示在页面中间 假设我们有一个div层:<div id=”myDiv”></div> 首先,我们用css来控制它在水平上始终居中,那么我们的css代码应该是这样 ...
- PHP 三元运算符省略写法
三元运算符 “?:” 又名条件运算符 表达式 (expr1) ? (expr2) : (expr3) 在 expr1 求值为 TRUE 时的值为 expr2,在 expr1 求值为 FALSE 时的值 ...
- mysql python image
连接mysql数据库: cnx = mysql.connector.connect(user='joe', database='test') Connector/Python参数列表 Argument ...
- FineUI第十七天---- 表格之扩展列
{ sb.AppendFormat(]); 1.通过表格的SelectedRowIndexArray获得选中行的索引号列表: 2.通过表格的DataKeys(二维数组)获取本行的数据 ...
- cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西。
cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西.