leetcode_c++:Divide and Conquer:The Skyline Problem(218)
来源:互联网 发布:人工智能产业链全景图 编辑:程序博客网 时间:2024/05/20 02:53
题目
class Solution {public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { vector< pair<int, int> > edges; //put all of edge into a vector //set left edge as negtive, right edge as positive //so, when we sort the edges, // 1) for same left point, the height would be descending order // 2) for same right point, the height would be ascending order int left, right, height; for(int i=0; i<buildings.size(); i++) { left = buildings[i][0]; right = buildings[i][1]; height = buildings[i][2]; edges.push_back(make_pair(left, -height)); edges.push_back(make_pair(right, height)); } sort(edges.begin(), edges.end()); // 1) if we meet a left edge, then we add its height into a `set`. // the `set` whould sort the height automatically. // 2) if we meet a right edge, then we remove its height from the `set` // // So, we could get the current highest height from the `set`, if the // current height is different with preivous height, then we need add // it into the result. vector< pair<int, int> > result; multiset<int> m; m.insert(0); int pre = 0, cur = 0; for (int i=0; i<edges.size(); i++){ pair<int,int> &e = edges[i]; if (e.second < 0) { m.insert(-e.second); }else{ m.erase(m.find(e.second)); } cur = *m.rbegin(); if (cur != pre) { result.push_back(make_pair(e.first, cur)); pre = cur; } } return result; }};
0 0
- leetcode_c++:Divide and Conquer:The Skyline Problem(218)
- LeetCode 218. The Skyline Problem--Divide and Conquer(分治法)
- leetcode_c++:Divide and Conquer:Expression Add Operators(282)
- leetcode_c++:Divide and Conquer:Burst Balloons(312)
- leetcode_c++:Divide and Conquer:Kth Largest Element in an Array(215)
- leetcode_c++:Divide and Conquer:. Search a 2D Matrix II(240)
- leetcode_c++:Divide and Conquer:Different Ways to Add Parentheses(241)
- leetcode_c++:Divide and Conquer:Count of Smaller Numbers After Self(315)
- leetcode_c++:Divide and Conquer:Count of Range Sum(327)
- the master theorem of divide-and-conquer
- 218 .The Skyline Problem
- 分治算法(divide and conquer)
- 分治法(divide-and-conquer)
- 514. Freedom Trail (Divide and Conquer)
- 169. Majority Element (divide and conquer)
- 53. Maximum SubArray(divide and conquer)
- 493. Reverse Pairs(divide and conquer)
- Divide and conquer algorithm
- 心得开始
- hive常用笔记
- spi nor flash驱动
- sysbench安装及常见问题解决
- VB程序学习代码记录20160828
- leetcode_c++:Divide and Conquer:The Skyline Problem(218)
- poj3167 Cow Patterns
- 生成学习算法之高斯判别分析模型
- Mybatis的<if>和<choose>的区别 以及“=”判断
- 备忘录模式(java版)
- 会计学
- javaFx的变量,javaFx的数据类型,javaFx的运算符
- ios学习开发0828
- HUD 1541/BIT(数状数组)