landscapemi的博客

# leetcode_c++:Divide and Conquer： The Skyline Problem（218）

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;

}
};

#### The Skyline Problem轮廓线问题算法详解

2017-06-02 15:18:27

#### poj 3044 City Skyline 单调栈

2017-02-28 17:03:15

#### leetcode 218: The Skyline Problem

2015-06-03 18:10:47

#### [leetcode] 218. The Skyline Problem 解题报告

2016-05-04 09:41:29

#### leetcode 218. The Skyline Problem 使用PriorityQueue的java实现 以及细致分析

2015-12-28 15:13:44

#### I - Skyline

2012-07-07 20:06:44

#### LeetCode 218. The Skyline Problem--Divide and Conquer(分治法)

2017-09-13 21:45:32

#### LeetCode218. The Skyline Problem分治法

2016-10-12 12:18:59

#### Leetcode 218 The Skyline Problem

2015-06-25 16:44:14

#### LeetCode218:The Skyline Problem

2015-08-04 11:09:06