218. The Skyline Problem

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).

Skyline Contour

The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ? Li, Ri ? INT_MAX, 0 < Hi ? INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .

The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].

Notes:

  • The number of buildings in any input list is guaranteed to be in the range [0, 10000].
  • The input list is already sorted in ascending order by the left x position Li.
  • The output list must be sorted by the x position.
  • There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]

一刷
思路:將線變?yōu)辄c[left, -h], [right, h], 并按照橫坐標從小到大sort阱州。
遍歷的時候左邊的點優(yōu)先出現(xiàn)褪尝,并加入height大在頂點的priority queue中颖对。
如果遍歷的時候鳖孤,h小于0微驶,表示為左頂點银舱,加入queue中屑咳。如果大于0,表示為右頂點柑蛇,從queue中移除芥挣。

public class Solution {
    //b: left, right, height
    public List<int[]> getSkyline(int[][] buildings) {
        List<int[]> res = new ArrayList<>();
        List<int[]> height = new ArrayList<>();
        for(int[] b:buildings){
            height.add(new int[]{b[0], -b[2]});
            height.add(new int[]{b[1], b[2]});
        }
        Collections.sort(height, new Comparator<int[]>(){
            public int compare(int[] a, int[] b){
                if(a[0]!=b[0]) return a[0] - b[0];
                return a[1] - b[1];
            }
        });
        
        Queue<Integer> pq = new PriorityQueue<>((a, b)->(b-a));//reverse
        pq.offer(0);//!!!!
        int prev = 0;
        for(int[] h : height){//可以保證左/右點出現(xiàn)時得到記錄
            if(h[1]<0) pq.offer(-h[1]);
            else pq.remove(h[1]);
            int cur = pq.peek();
            if(prev!=cur){
                res.add(new int[]{h[0], cur});
                prev = cur;
            }
        }
        return res;
    }
}

Speed Up

class Solution {
    class KeyPoint {
        public int key;
        public int height;
        public KeyPoint next = null;

        public KeyPoint(int key, int height) {
            this.key = key;
            this.height = height;
        }

    }

    public List<int[]> getSkyline(int[][] buildings) {
        KeyPoint head = new KeyPoint(-1,0);
        KeyPoint prevKP = head;
        for (int[] building:buildings) {
            int l = building[0], r = building[1], h= building[2];
            // insert left point
            while (prevKP.next != null && prevKP.next.key <= l) prevKP = prevKP.next;
            int preHeight = prevKP.height;
            if (prevKP.key == l) prevKP.height = Math.max(prevKP.height, h);
            else if (prevKP.height < h) {
                KeyPoint next = prevKP.next;
                prevKP.next = new KeyPoint(l, h);
                prevKP = prevKP.next;
                prevKP.next = next;
            }
            // insert right point and update points in between
            KeyPoint prev = prevKP, cur = prevKP.next;
            while (cur != null && cur.key < r) {
                preHeight = cur.height;
                cur.height = Math.max(cur.height, h);
                if (cur.height == prev.height)
                    prev.next = cur.next;
                else
                    prev = cur;
                cur = cur.next;
            }
            if (prev.height != preHeight && prev.key != r && (cur == null || cur.key != r)) {
                KeyPoint next = prev.next;
                prev.next = new KeyPoint(r, preHeight);
                prev.next.next = next;
            }
        }
        // convert to List<int[]>
        List<int[]> list = new ArrayList<int[]>();
        KeyPoint prev = head, cur = head.next;
        while (cur != null) {
            if (cur.height != prev.height)
                list.add(new int[]{cur.key, cur.height});
            prev = cur;
            cur = cur.next;
        }
        return list;
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市耻台,隨后出現(xiàn)的幾起案子空免,更是在濱河造成了極大的恐慌,老刑警劉巖盆耽,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鼓蜒,死亡現(xiàn)場離奇詭異,居然都是意外死亡征字,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進店門娇豫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來匙姜,“玉大人,你說我怎么就攤上這事冯痢〉粒” “怎么了?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵浦楣,是天一觀的道長袖肥。 經(jīng)常有香客問我,道長振劳,這世上最難降的妖魔是什么椎组? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮历恐,結果婚禮上寸癌,老公的妹妹穿的比我還像新娘专筷。我一直安慰自己,他們只是感情好蒸苇,可當我...
    茶點故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布磷蛹。 她就那樣靜靜地躺著,像睡著了一般溪烤。 火紅的嫁衣襯著肌膚如雪味咳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天檬嘀,我揣著相機與錄音槽驶,去河邊找鬼。 笑死枪眉,一個胖子當著我的面吹牛捺檬,可吹牛的內容都是我干的。 我是一名探鬼主播贸铜,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼堡纬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蒿秦?” 一聲冷哼從身側響起烤镐,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎棍鳖,沒想到半個月后炮叶,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡渡处,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年镜悉,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片医瘫。...
    茶點故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡侣肄,死狀恐怖,靈堂內的尸體忽然破棺而出醇份,到底是詐尸還是另有隱情稼锅,我是刑警寧澤,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布僚纷,位于F島的核電站矩距,受9級特大地震影響,放射性物質發(fā)生泄漏怖竭。R本人自食惡果不足惜锥债,卻給世界環(huán)境...
    茶點故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赞弥,春花似錦毅整、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至拼窥,卻和暖如春戏蔑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背鲁纠。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工总棵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人改含。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓情龄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親捍壤。 傳聞我的和親對象是個殘疾皇子骤视,可洞房花燭夜當晚...
    茶點故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,437評論 0 23
  • 日記展示:8班 寫作源于生活,只有做生活的有心人鹃觉,才能發(fā)現(xiàn)生活中的美好专酗,有發(fā)自內心的感悟。 張晶同學日記 2017...
    筱華de小天地閱讀 500評論 0 2
  • 昔日化泥始護花盗扇, 奈何花開不顯泥祷肯, 待到花落芳飛盡, 春泥初醒永護花疗隶。
    踏浪散人閱讀 257評論 0 0
  • 這是第二次抱著手機在夜里寫一些話佑笋,第一次是寫關于自己喜歡的人,寫了又刪斑鼻,刪了又寫蒋纬,終是無果。這次卵沉,想寫寫朋友的事...
    我是雪娃娃閱讀 517評論 0 1
  • 今年之初,給自己定了幾個規(guī)劃法牲。如今堅持下來的有四個半史汗,分別是:寫作,讀書拒垃,聽喜馬拉雅停撞,做飯,運動(算半個)。 ...
    十年一井閱讀 94評論 1 1