sweep line-The Skyline Problem
2020-01-10 17:51:05
问题描述:

问题求解:
本题是经典的sweep line问题。
对于sweep line问题我们需要考虑的只有两点:
1. 延水平方向 / 时间方向 :时间队列 event queue,一般来说是一个优先队列;
2. 延垂直方向 :sweep line status,即当前的扫描线的状态,一般会将交点按照顺序排序;
对于本题来说,sweep line status可以使用一个multi set来进行维护,当然由于在Java中没有multi set,因此需要使用TreeMap来模拟。
event queue的当然是使用优先队列,问题是如何进行排序,这个才是本题的核心难点。
这里给出结论:
大方向是按照x轴排序,如果x轴相等那么按照height排序;
如果x轴相等,优先判断是否是左端点,如果是左端点,那么优先入队;
如果同时是右端点,那么需要反序入队,就是height小的反而需要排在前面。
public List<List<Integer>> getSkyline(int[][] buildings) {
List<List<Integer>> res = new ArrayList<>();
// 对于同x轴,优先将左端点入队列
// 如果同是右端点,则要反序,小的先入队列
// 其余按照正常的height顺序排列即可
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>(){
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0] && o1[2] != o2[2]) return o1[2] - o2[2];
if (o1[0] == o2[0] && o1[2] == 1 && o1[2] == o2[2]) return o1[1] - o2[1];
return o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0];
}
});
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int[] b : buildings) {
int s = b[0];
int e = b[1];
int h = b[2];
pq.add(new int[]{s, h, 0});
pq.add(new int[]{e, h, 1});
}
while(!pq.isEmpty()) {
int[] event = pq.poll();
int x = event[0];
int h = event[1];
int status = event[2];
int curr_max = map.size() == 0 ? 0 : map.lastKey();
if (status == 0) {
if (h > curr_max) res.add(Arrays.asList(x, h));
map.put(h, map.getOrDefault(h, 0) + 1);
}
else {
map.put(h, map.get(h) - 1);
if (map.get(h) == 0) map.remove(h);
curr_max = map.size() == 0 ? 0 : map.lastKey();
if (h > curr_max) res.add(Arrays.asList(x, curr_max));
}
}
return res;
}
sweep line-The Skyline Problem的更多相关文章
- 218. The Skyline Problem *HARD* -- 矩形重叠
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- Sweep Line
391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/ ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [LeetCode] The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- 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] 281. 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 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- 218. The Skyline Problem
题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...
随机推荐
- 每日背单词 - Jun.
6月1日裸辞,计划休息到端午节后,这段时间玩的确实很开心,每天和朋友一起吹灯拔蜡:好不自在,可惜假期马上结束了,从今天开始恢复学习状态. 2018年6月1日 - 2018年6月14日 辞职休假 201 ...
- 【C++基础】008常量和变量
简介:常量和变量. 常量和变量 1. 常量 具体把数据写出来 2,3,4: 1.2,1.3: "Hello World!","C++": cout <&l ...
- 【ThinkPHP6:从TP3升级到放弃】1. 前言及准备工作
春节期间因为疫情的关系出不去门,所以就研究了一下ThinkPHP的最新版本6.0.2, 自己写了一个博客程序. 现在, 打算写一个ThinkPHP6的专题, 用来把自己在写博客的过程中入过的坑和获得的 ...
- 8421BCD转余3码Verilog HDL的设计(1)
近期阅读Verilog HDL高级数字设计(第二版)中,遇到了串行比特流BCD码转余3码转换器的设计,比较独特的是: (1)该转换器的输入为1位串行比特流,输出也为1位串行比特流. BCD码与余三码的 ...
- CSS中"position:relative"属性与文档流的关系
前言 近期遇到一个问题--"position:relative"到底会不会导致元素脱离文档流?主流观点是不会,但都给不出一个有说服力的论据.最后我自己佐证了一番,总算有了个结果:& ...
- JavaScript的类数组
类数组对象啊,被人问到它跟真正的数组对象有什么差别啊?说不上来就老埋汰了,只知道函数的arguments对象是个类数组对象,也有length属性,其他呢?干货奉上: 首先先说说数组吧: 1,当有新的元 ...
- MySQL的万字总结(缓存,索引,Explain,事务,redo日志等)
hello,小伙伴们,好久不见,MySQL系列停更了差不多两个月了,也有小伙伴问我为啥不更了呢?其实我去看了MySQL的全集,准备憋个大招,更新篇长文(我不会告诉你是因为我懒的). 好了,话不多说,直 ...
- 本地Hadoop集群搭建
什么是Hadoop? Hadoop是一个开源的Apache项目,通常用来进行大数据处理. Hadoop集群的架构: master节点维护分布式文件系统的所有信息,并负责调度资源分配,在Hadoop集群 ...
- List remove ConcurrentModificationException源码分析
代码块 Java Exception in thread "main" java.util.ConcurrentModificationException at j ...
- Python只有文件不存在才能写文件
当我们在Python里面写文件时,我们常用的模式为 w模式,这种模式下,如果文件不存在,就会生成文件:如果文件已经存在,就会直接覆盖. 有时候,如果文件已经存在,直接覆盖文件可能会导致重要数据丢失.你 ...