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 ...
随机推荐
- Face Recognition 人脸识别该如何测试
猪圈子,一个有个性的订阅号 01 测量人脸识别的主要性能指标有 1.误识率(False;Accept;Rate;FAR):这是将其他人误作指定人员的概率; 2.拒识率(False;RejectRate ...
- RocketMQ borker配置文件
master节点:serverSelectorThreads = 3 brokerRole = SYNC_MASTER serverSocketRcvBufSize = 131072 osPageCa ...
- Allenmind's Blog
听说,Sass和Compass更配哟.来看看Compass的基本用法! 目录 Compass和Sass 安装Compass 项目初始化 编译 Compass的模块 Compass的Helper函数 一 ...
- 在 React Native 中使用 moment.js 無法載入語系檔案
moment.js 是很常見的日期時間 library,友善的 API 與極佳的執行效率是它的兩大賣點.例如 (new Date()).getFullYear(),如果使用 moment.js 我可以 ...
- PyCharm+git+码云实现project版本控制
1.安装git https://git-scm.com/downloads 2.PyCharm中配置 3.申请码云 4.PyCharm中安装码云插件 右键选择,重启Pycharm. 重新打开PyCha ...
- WordPress鼠标点击特效和粒子插件
鼠标特效 将下面代码复制到外观-主题编辑器-主题页脚(footer.php)代码<?php wp_footer(); ?>上方. <script type="text/ja ...
- 01 Taro_Mall 开源多端小程序框架设计
项目介绍 Taro_Mall是一款多端开源在线商城应用程序,后台是基于litemall基础上进行开发,前端采用Taro框架编写,现已全部完成小程序和h5移动端,后续会对APP,淘宝,头条,百度小程序进 ...
- [LeetCode] 1103. Distribute Candies to People 分糖果
题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5 ...
- 题解-[HNOI2001]遥控赛车比赛
题解-[HNOI2001]遥控赛车比赛 前置知识:记忆化搜索.\(\texttt{Bfs}\). 参考资料 https://www.luogu.com.cn/blog/CYJian/solution- ...
- Python-Pyquery库的安装和调用
解析库pyquery:# pip安装pyquery库pip3 install pyquery from pyquery import PyQuery as pq # 定义doc,输入html源代码 d ...