leetcode_c++:Divide and Conquer: The Skyline Problem(218)

原创 2016年08月28日 15:28:50

题目


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;

    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode 218: The Skyline Problem

leetcode 218: The Skyline Problem java python c++
  • xudli
  • xudli
  • 2015年06月03日 18:10
  • 4523

leetcode_c++:Divide and Conquer:Count of Smaller Numbers After Self(315)

You are given an integer array nums and you have to return a new counts array. The counts array has ...

[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...
  • sbitswc
  • sbitswc
  • 2015年10月08日 14:51
  • 1308

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

题目链接: https://leetcode.com/problems/the-skyline-problem/ A city's skyline is the outer contour of ...

[leetcode] 218.The Skyline Problem

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

leetcode 218. The Skyline Problem java代码

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

Leetcode 218. The Skyline Problem[hard]

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

LeetCode218. The Skyline Problem分治法

题意:轮廓问题,不好描述。首先怎么分: 不停对半分 停止条件1:为空时,直接返回空 停止条件2:大小为1时,直接返回左上角点和右下角点。 那么怎么合并? 维护两个变量:l,r l为左半部分当前位置,r...

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

LeetCode 218. The Skyline Problem(天际线)

原题网址:https://leetcode.com/problems/the-skyline-problem/ A city's skyline is the outer contour of t...
  • jmspan
  • jmspan
  • 2016年05月06日 07:03
  • 681
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_c++:Divide and Conquer: The Skyline Problem(218)
举报原因:
原因补充:

(最多只允许输入30个字)