天际线问题,参考自: 百草园

天际线为当前线段的最高高度,所以用最大堆处理,当遍历到线段右端点时需要删除该线段的高度,priority_queue不提供删除的操作,要用unordered_map来标记要删除的元素。从heap中pop的时候先看有没有被标记过,如果标记过,就一直pop直到空或都找到没被标记过的值(之前的值被标记过也要删除干净,因为到当前x坐标时,标记过的高度已失效)。为排除冗余答案,需要提前对线段排序,横坐标相等时,都是左端点,按y从大到小(只记录高的端点),都是右端点,按y从小到大(只记录高的端点),一左一右,左在前右在后,不重复记录。

 class Solution {
private:
enum NODE_TYPE {LEFT, RIGHT};
struct node{
int x,y;
NODE_TYPE type;
node(int _x, int _y, NODE_TYPE _type): x(_x),y(_y),type(_type){}
}; public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
vector<node> height;
for(int i=;i<buildings.size();i++){
height.push_back(node(buildings[i][],buildings[i][],LEFT));
height.push_back(node(buildings[i][],buildings[i][],RIGHT));
}
sort(height.begin(),height.end(),[](const node &a, const node& b) {
if(a.x!=b.x)return a.x<b.x;
else if(a.type==b.type && a.type == LEFT) return a.y>b.y;
else if(a.type==b.type && a.type == RIGHT) return a.y<b.y;
else return a.type == LEFT;
});
priority_queue<int> heap;
heap.push();
unordered_map<int,int> mp; // remove the element in heap
int cur=,pre=;
vector<vector<int>> res;
for(auto & h : height){
if(h.type == LEFT){
heap.push(h.y);
} else {
mp[h.y]++;
while(!heap.empty()&&mp[heap.top()]>) {
mp[heap.top()]--;
heap.pop();
}
}
cur = heap.top();
if(cur!=pre){
res.push_back({h.x,cur});
pre = cur;
}
}
return res;
}
};

218. The Skyline Problem (LeetCode)的更多相关文章

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

  2. [LeetCode#218] The Skyline Problem

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

  3. 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 whe ...

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

  5. 218. The Skyline Problem

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

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

  7. The Skyline Problem leetcode 详解

    class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>&g ...

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

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

  9. [LeetCode] The Skyline Problem

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

随机推荐

  1. UFUN函数 UF_ASSEM UF_PART函数(UF_ASSEM_ask_work_part,UF_PART_ask_part_name)

    UF_initialize(); tag_t work_part_tag=NULL_TAG; ]=""; //获取当前工作部件的tag work_part_tag=UF_ASSEM ...

  2. bzoj 3992: [SDOI2015]序列统计 NTT+原根

    今天开始学习丧心病狂的多项式qaq......    . code: #include <bits/stdc++.h> #define ll long long #define setIO ...

  3. [Cqoi2016]K远点对 K-Dtree

    4520: [Cqoi2016]K远点对 链接 bzoj 思路 用K-Dtree求点的最远距离. 求的时候顺便维护一个大小为2k的小根堆. 不知道为啥一定会对. 代码 #include <bit ...

  4. 【数论】[因数个数]P4167樱花

    题目描述 求不定方程 \(\frac {1}{x} + \frac{1}{y} = \frac{1}{n!}\)的正整数解的个数 \(n \leq 100^6\) Solution 化简得 \(x * ...

  5. mysql课外积累

    where 与 on 的区别 : ON:针对关联表进行条件筛选,不会影响结果集的数量和主表数据. WHERE:针对结果集进行条件筛选,会影响结果集的数量. LIKE声明中的%和_是什么意思? --%对 ...

  6. 下拉选择的blur和click事件冲突了

    当写个下拉选择框时我们希望当input失去焦点时,下拉框消失,或者当选择下拉框中的内容的同时将内容填入input并且使下拉框消失. 这时候我们会想到blur和click,单独使用的时候是没有问题的,但 ...

  7. nginx 访问控制之 location

    在生产环境中,我们会对某些特殊的请求进行限制,比如对网站的后台进行限制访问. 这就用到了location配置. 示例1: location /aming/ { deny all; } 说明:针对/am ...

  8. kafka(一)设计分析

    参考文档:Kafka 设计与原理详解:http://blog.csdn.net/suifeng3051/article/details/48053965Kafka深度解析:http://blog.cs ...

  9. NIOBuffer 缓冲区

    Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但是,Channel,Buffer 和 Select ...

  10. Redis缓存穿透、缓存雪崩、redis并发问题 并发竞争key的解决方案 (阿里)

    阿里的人问我 缓存雪崩(大量数据在同一时间过期了)了如何处理,缓存击穿了如何处理,回答的很烂,做了总结: 把redis作为缓存使用已经是司空见惯,但是使用redis后也可能会碰到一系列的问题,尤其是数 ...