首页 > 其他分享 >[leetcode] The Skyline Problem

[leetcode] The Skyline Problem

时间:2022-11-02 02:00:12浏览次数:46  
标签:pq int max add height Skyline new Problem leetcode

 

 

class Solution {
    public List<int[]> getSkyline(int[][] buildings) {
        List<int[]> res = new ArrayList<>();
        List<int[]> height = new ArrayList<>();
        for (int[] building : buildings) {
            // start point has negative height value
            height.add(new int[]{building[0], -building[2]});
            // end point has normal height value
            height.add(new int[]{building[1], building[2]});
        }
        Collections.sort(height, new Comparator<int[]>() {
            @Override
            public int compare(int[] a, int[] b) {
                if (a[0] == b[0]) {
                    return a[1] - b[1];
                } else {
                    return a[0] - b[0];
                }
            }
        });
        // Use a maxHeap to store possible heights
        // But priority queue does not support remove in lgn time
        // treemap support add, remove, get max in lgn time, so use treemap here
        // key: height, value: number of this height
        TreeMap<Integer, Integer> pq = new TreeMap<>();
        pq.put(0, 1);
        // Before starting, the previous max height is 0;
        int prev = 0;
        // visit all points in order
        for (int[] h : height) {
            // a start point, add height
            if (h[1] < 0) {
                pq.put(-h[1], pq.getOrDefault(-h[1], 0) + 1);
            } else {  // a end point, remove height
                if (pq.get(h[1]) > 1) {
                    pq.put(h[1], pq.get(h[1]) - 1);
                } else {
                    pq.remove(h[1]);
                }
            }
            int cur = pq.lastKey();
            // compare current max height with previous max height, update result and 
            // previous max height if necessary
            if (cur != prev) {
                res.add(new int[]{h[0], cur});
                prev = cur;
            }
        }
        return res;
    }
}

 

https://www.youtube.com/watch?v=GSBLe8cKu0s 

https://leetcode.com/problems/the-skyline-problem/discuss/61193/Short-Java-solution

 

标签:pq,int,max,add,height,Skyline,new,Problem,leetcode
From: https://www.cnblogs.com/jdflyfly/p/16849728.html

相关文章

  • LeetCode刷题记录.Day3
    长度最小的子数组题目链接209.长度最小的子数组-力扣(LeetCode)看似很简单。看完滑动窗口法的时候觉得很容易理解,时间复杂度O(n)的推导也理解。无非就是两个指针,因为题......
  • leetcode-2423-easy
    RemoveLetterToEqualizeFrequencyYouaregivena0-indexedstringword,consistingoflowercaseEnglishletters.Youneedtoselectoneindexandremovethe......
  • leetcode-2144-easy
    MinimumCostofBuyingCandiesWithDiscountAshopissellingcandiesatadiscount.Foreverytwocandiessold,theshopgivesathirdcandyforfree.Thec......
  • leetcode-1460-easy
    MakeTwoArraysEqualbyReversingSubarraysYouaregiventwointegerarraysofequallengthtargetandarr.Inonestep,youcanselectanynon-emptysubarra......
  • leetcode-257-easy
    BinaryTreePathsGiventherootofabinarytree,returnallroot-to-leafpathsinanyorder.Aleafisanodewithnochildren.Example1:Input:root=......
  • leetcode-1304-easy
    FindNUniqueIntegersSumuptoZeroGivenanintegern,returnanyarraycontainingnuniqueintegerssuchthattheyaddupto0.Example1:Input:n=5O......
  • leetcode-118-easy
    Pascal'sTriangleGivenanintegernumRows,returnthefirstnumRowsofPascal'striangle.InPascal'striangle,eachnumberisthesumofthetwonumbersdir......
  • leetcode-1137-easy
    N-thTribonacciNumberTheTribonaccisequenceTnisdefinedasfollows:T0=0,T1=1,T2=1,andTn+3=Tn+Tn+1+Tn+2forn>=0.Givenn,returnthe......
  • leetcode111-二叉树的最小深度
    111.二叉树的最小深度 这道题相比 104.二叉树的最大深度 还是难上一些的,但也不算太难。BFS/***Definitionforabinarytreenode.*structTreeNode{......
  • leetcode-2. 两数相加
    题目描述给你两个 非空的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。请你将两个数相加,并以相同形式返回一......