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的更多相关文章

  1. 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 ...

  2. Sweep Line

    391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/ ...

  3. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  4. [LeetCode] The Skyline Problem

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  5. The Skyline Problem

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  6. [LeetCode#218] The Skyline Problem

    Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...

  7. [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 ...

  8. [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 ...

  9. 218. The Skyline Problem

    题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...

随机推荐

  1. Docker Container开机自动启动

    重启策略: 使用在Docker run的时候使用--restart参数来设置. no - Container不重启 on-failure - container推出状态非0时重启 always - 始 ...

  2. [PyTorch入门之60分钟入门闪击战]之训练分类器

    训练分类器 目前为止,你已经知道如何定义神经网络.计算损失和更新网络的权重.现在你可能在想,那数据呢? What about data? 通常,当你需要处理图像.文本.音频或者视频数据时,你可以使用标 ...

  3. Mac下好玩的终端命令

    figlet brew install figlet cowsay brew install cowsaycowsay -l: 查看所有可用动物cowsay -f daemon hello world ...

  4. 蚂蚁金服招聘-无线测试开发(20k-36k/月)

    蚂蚁金服-支付宝国际事业部-高级测试开发工程师/测试专家 工作年限:三年以上学历要求:本科期望层级:P6/P7工作地点:上海,杭州,深圳等为什么选择加入我们? 我们的岗位有何不同?1.国际化远景:随着 ...

  5. 爬虫入门(四):urllib2

    主要使用python自带的urllib2进行爬虫实验. 写在前面的蠢事:本来新建了一个urllib2.py便于好认识这是urllib2的实验,结果始终编译不通过,错误错误.不能用Python的关键字( ...

  6. 基于activity的强大java工作流引擎,可视化开发工作流

    我们先来看看工作流引擎和Activity? 工作流引擎 所谓工作流引擎是指workflow作为应用系统的一部分,并为之提供对各应用系统有决定作用的根据角色.分工和条件的不同决定信息传递路由.内容等级等 ...

  7. p标签内不能嵌套块级标签

    今天突然发现一个问题,那就是p标签内不能嵌套块级标签 例如: <p><p></p></p> 会被浏览器解析成 我又把 div 嵌套在里面,发现还是这样 ...

  8. 正式学习MVC 05

    1.剃须刀模板razor的使用 1)混编 循环语法 @model List<MVCStudy.Models.Student> @{ ViewBag.Title = "List&q ...

  9. htmlhint 规则详解

    HTML 静态检查规则 HTMLHint 工具内置 23 条规则,可以对 HTML 代码文件进行静态代码检查,从而提高 HTML 代码编写的规范和质量.现在把 23 条规则翻译如下. 一.规则列表 标 ...

  10. JavaScript的数组(一)

    在JavaScript中,对象,数组,函数是最最常用的东东了,写完了对象和函数,最后来说说数组吧,提到数组,就只能想到,map,forEach啊,pop,push啊,当真是没有一点点的积累了?这么多年 ...