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

天际线为当前线段的最高高度,所以用最大堆处理,当遍历到线段右端点时需要删除该线段的高度,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. cc2530的I/O中断

    通用I/O的中断 cc2530的CPU有18个中断源,每个中断都可以分别使能和控制. 18个中断源的优先级 18个中断源分为6个组,每一组有3个中断源,中断优先级可以通过配置相应寄存器来实现 中断源的 ...

  2. Vue的SEO问题汇总

    方式一 思否 https://segmentfault.com/q/1010000011824706 SSR 和 Nuxt.js : https://zh.nuxtjs.org/ https://se ...

  3. UDS的使用

    我们通过对导热微分方程式的求解,并与Fluent自己的求解结果进行对比,介绍一下Fluent当中UDS(自定义标量)的具体使用方法. 首先Fluent当中的UDS主要针对下面这样形式的方程: 其中: ...

  4. vi编辑器操作 快捷键

    vi编辑器操作 快捷键 1. 命令模式 与 编辑模式切换 a:光标向后移动一位进入编辑模式 i:光标和内容 没有变化进入编辑模式 o:新起一行进入编辑模式 s:删除光标所在字符进入编辑模式       ...

  5. Metasploit使用内网跳板, 扫描局域网主机

    最近,拿到一台内网机器, 苦于无法使用nmap扫描改主机的内网, 所以才有此文 在跳板机子获取一定权限后,需要积极的向内网主机权限发展,获取指定的目标信息,探查系统漏洞,借助msf已经得到的meter ...

  6. vue-devtools 获取到 vuex store 和 Vue 实例的?

    vue-devtools 获取到 vuex store 和 Vue 实例的? https://github.com/vuejs/vue-devtools       安装了 vue-devTools ...

  7. ROC曲线 VS PR曲线

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&ut ...

  8. CRM 负责人 选择

    CRM 负责人 参与 - 搜狗搜索https://www.sogou.com/sgo?query=CRM+%E8%B4%9F%E8%B4%A3%E4%BA%BA+%E5%8F%82%E4%B8%8E& ...

  9. Nginx返回固定json或者文本格式的方法

    在系统还没有做集群的情况下,直接重启项目时刚好用户在使用的话,一般都会受到投诉,那么使用nginx返回类似“系统维护”的提示信息并且提前在应用上面做通知才是合适的做法 那么记录一下nginx里面的配置 ...

  10. hutool JAVA 工具类

    https://hutool.cn/docs/#/ 入门和安装 A set of tools that keep Java sweet. -- 主页:https://hutool.cn/ | http ...