強化四 掃描線

掃描問題的特點
1 事件往往是以區間的形式存在
2 區間兩端代表事件的開始和結束
3 按照區間起點排序,起點相同的按照終點拍排序

掃描線要點
將起點和終點打散排序
[[1,3], [2,4]] => [[1,start],[2,start],[3,end],[4,end]]

391 Number of Airplanes in the Sky

  • 降落-1 起飛+1
  • 遇到一個-1總數-1 遇到一個+1總數+1
  • 因為題目說同時有起飛降落時先考慮降落 所以排序時降落的在前

218 The Skyline Problem

  • 花花醬 LeetCode 218. The Skyline Problem講解的非常好

todo
919 Meeting Rooms II
821 Time Intersection

lt391 Number of Airplanes in the Sky

class Pair{
    int index;
    int delta;
    Pair(int index, int delta){
        this.index = index;
        this.delta = delta;
    }
}

public class Solution {
    /**
     * @param airplanes: An interval array
     * @return: Count of airplanes are in the sky.
     */
    public int countOfAirplanes(List<Interval> airplanes) {
        // write your code here
        if(airplanes==null || airplanes.size()==0)
            return 0;
        List<Pair> list = new ArrayList<>();
        for(Interval interval : airplanes){
            list.add(new Pair(interval.start, 1));
            list.add(new Pair(interval.end, -1));
        }
        Comparator<Pair> com = new Comparator<Pair>(){
            public int compare(Pair a, Pair b){
                if(a.index!=b.index){
                    return a.index-b.index;
                }else{
                    return a.delta-b.delta;
                }
            }
        };
        Collections.sort(list, com);
        int count = 0;
        int max = Integer.MIN_VALUE;
        for(Pair pair : list){
            count = count + pair.delta;
            max = Math.max(max, count);
        }
        return max;
    }
}

218 The Skyline Problem

class Solution {
    class Event{
        int height;
        int eventType;
        int index;
        Event(int height, int eventType, int index){
            this.height = height;
            //0 entering; 1 leaving
            this.eventType = eventType;
            this.index = index;
        }
    }

    public List<int[]> getSkyline(int[][] buildings) {
        List<int[]> results = new ArrayList<>();
        if(buildings==null || buildings.length==0 || buildings[0]==null || buildings[0].length==0)
            return results;
        List<Event> list = new ArrayList<>();
        for(int i=0; i<buildings.length; i++){
            list.add(new Event(buildings[i][2], 0, buildings[i][0]));
            list.add(new Event(buildings[i][2], 1, buildings[i][1]));
        }
        System.out.println(list.size());
        Comparator<Event> com = new Comparator<Event>(){
            public int compare(Event a, Event b){
                if(a.index!=b.index)
                    //坐標不同先按處理坐標小的
                    return a.index-b.index;
                if(a.eventType!=b.eventType){
                    //事件類型不同時 先考慮進入事件
                    return a.eventType-b.eventType;
                }
                if(a.eventType==0){
                    //都是進入事件時 先處理高的
                    return b.height-a.height;
                }
                //都是離開事件時 先處理高的
                return a.height-b.height;
            }
        };
        Collections.sort(list, com);

        Queue<Integer> maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
        maxHeap.offer(0);
        for(Event event : list){
            //答案只會出現在有新的大樓進入 或者有大樓離開
            if(event.eventType==0){
                //是進入事件
                if(maxHeap.peek()<event.height){
                    int[] result = {event.index, event.height};
                    results.add(result);
                }
                maxHeap.offer(event.height);
            }else{
                //是離開事件
                maxHeap.remove(event.height);
                if(maxHeap.peek()<event.height){
                    int[] result = {event.index, maxHeap.peek()};
                    results.add(result);
                }
            }
        }
        return results;
    }
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,768評論 0 33
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,424評論 0 10
  • 自尊心太強會有兩種結果,不在沉默中滅亡,就在沉默中爆發。 過于敏感,過于陰暗,不愿意把別人往美好光明的地方去...
    嵐楓紫諾閱讀 109評論 0 0
  • 學習如何打造digital learning environment,后臺做個flyer要了親命了! 自己的版本,...
    Redfever閱讀 494評論 1 2
  • 我愛你們,我恨你們。 對不起,爸,媽,對不起寶貝兒。我想走了,我不喜歡這里了。 我聽人說,要學會看到世界的美好,我...
    祭酒老六閱讀 981評論 0 5