Skyline Problem (Leetcode 218, Lintcode 131)

一道经典的面试题。这道题在Leetcode上被列为Hard,而在Lintcode上被列为Super,不过掌握要点后就还好。

要点

  1. c++中,用multiset建立priority_queue, 以便于删除

  2. 利用扫描线的算法,将输入拆分成起点终点段。扫描时,如果是起点,加入pque,如果是终点,则将该点高度从pque中删除。而每次遇到一个点,都从pque中取出最大值process。

  3. 如果是起点,就把高度按照负数存放,这是为了处理overlap的情况。同一个点的起点,应该由高到低排序。同一个点的终点,应该由低到高排序。同时,先起点,再终点。

multiset的用法:

multiset<int, greater<int>> st; //max heap建立
st.erase(st.find(20)) // 删除
class Solution {
public:
    
    struct Edge{
       int x, height;
       bool isStart;
       Edge(int x_, int h_, bool i_) : x(x_), height(h_), isStart(i_){}
    };
    
    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
        vector<pair<int, int>> ret;
        if(buildings.empty()) return ret;
        vector<Edge> Edges;
        for(auto it : buildings){
            Edges.push_back(Edge(it[0], -it[2], true));
            Edges.push_back(Edge(it[1], it[2], false));
        }
        
        auto comp = [](const Edge &e1, const Edge &e2){
            if(e1.x == e2.x){
                return e1.height < e2.height;
            }
            return e1.x < e2.x;
        };
        
        sort(Edges.begin(), Edges.end(), comp);
        
        multiset<int, greater<int>> st;
        int pre = 0;
        for(auto it : Edges){
            if(it.isStart){
                st.insert(-it.height);
            }
            else{
                st.erase(st.find(it.height));
            }
            int cur = *st.begin();
            if(cur != pre){
                ret.push_back({it.x, cur});
                pre = cur;
            }
        }
        return ret;
        
    }
};

Lintcode的要求更高一些,需要输出新的skyline边,而不是skyline点。而要点就是新增一个变量,来记录上一个点的x坐标

class Solution {
public:
    /**
     * @param buildings: A list of lists of integers
     * @return: Find the outline of those buildings
     */
    struct Edge{
       int x, height;
       bool isStart;
       Edge(int x_, int h_, bool i_) : x(x_), height(h_), isStart(i_){}
    };
    vector<vector<int>> buildingOutline(vector<vector<int>> &buildings) {
        // write your code here
        vector<vector<int>> ret;
        if(buildings.empty()) return ret;
        
        vector<Edge> Edges;
        for(auto it : buildings){
            Edges.push_back(Edge(it[0], -it[2], true));
            Edges.push_back(Edge(it[1], it[2], false));
        }
        
        auto comp = [](const Edge &e1, const Edge &e2){
            if(e1.x == e2.x){
                return e1.height < e2.height;
            }
            return e1.x < e2.x;
        };
        
        sort(Edges.begin(), Edges.end(), comp);
        multiset<int, greater<int>> st;
        int pre = 0, prex = 0;
        
        for(auto e : Edges){
            if(e.isStart){
                st.insert(-e.height);
            }
            else{
                st.erase(st.find(e.height));
            }
            int cur = *st.begin();
            if(cur != pre){
                if(pre != 0){
                    ret.push_back({prex, e.x, pre});
                }
                prex = e.x;
                pre = cur;
            }
        }
        return ret;
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,356评论 0 33
  • 原文发表在我的博客:大楼轮廓实现及优化求关注、求交流、求意见、求建议。 问题 LintCode:大楼轮廓 描述 水...
    華方阅读 5,870评论 0 8
  • 教你如何迅速秒杀掉:99%的海量数据处理面试题 本文经过大量细致的优化后,收录于我的新书《编程之法》第六章中,新书...
    Helen_Cat阅读 12,123评论 1 39
  • 第一章 绪论 什么是数据结构? 数据结构的定义:数据结构是相互之间存在一种或多种特定关系的数据元素的集合。 第二章...
    SeanCheney阅读 11,101评论 0 19
  • 中餐or晚餐 南京大牌档 这个饭店每个地方几乎都有,就是大概人蛮多需要提前去最好,美团都有,招牌菜上面也有写 外婆...
    酒红狂嬷嬷阅读 1,810评论 0 0

友情链接更多精彩内容