摘要:復(fù)雜度思路利用的思想,先分成左右兩部分,再進(jìn)行。每次都要將的左上角和右下角推進(jìn),進(jìn)行計(jì)算。觀察左邊和右邊進(jìn)行。
LeetCode[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).
Divide & ConquerThe 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 itsIt is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX,
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] ] .
復(fù)雜度
O(NlgN), O(N)
思路
利用merge sort的思想,先分成左右兩部分,再進(jìn)行merge。每次都要將building的左上角和右下角推進(jìn)priorityQueue,進(jìn)行計(jì)算。觀察左邊和右邊進(jìn)行merge。
代碼
public ListgetKyLine(int[][] buildings) { return merge(buildings, 0, buildings.length - 1); } public LinkedList merge(int[][] buildings, int lo, int hi) { LinkedList res = new LinkedList<>(); if(lo > hi) { return res; } else if(lo == hi) { res.add(new int[]{buildings[lo][0], buildings[lo][2]}; res.add(new int[]{buildings[lo][0], 0}); return res; } int mid = lo + (hi - lo) / 2; LinkedList left = merge(buildings, lo, mid); LinkedList right = merge(buildings, mid + 1, hi); int leftH = 0, rightH = 0; while(!left.isEmpty() || !right.isEmpty()) { long x1 = left.isEmpty() ? Long.MAX_VALUE : left.peekFirst()[0]; long x2 = right.isEmpty() ? Long.MAX_VALUE : right.peekFirst()[0]; int x = 0; if(x1 < x2) { int[] temp = left.pollFirst(); x = temp[0]; leftH = temp[1]; } else if(x1 > x2) { int[] temp = right.pollFirst(); x = temp[0]; rightH = temp[1]; } else { x = left.peekFirst()[0]; leftH = left.pollFirst()[1]; rightH = right.pollFirst()[1]; } int h = Math.max(leftH, rightH); if(res.isEmpty() || h != res.peekFirst()[1]) { res.add(new int[]{x, h}); } return res; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/66137.html
摘要:遍歷時(shí),通過一個(gè)堆來(lái)得知當(dāng)前圖形的最高位置。堆頂是所有頂點(diǎn)中最高的點(diǎn),只要這個(gè)點(diǎn)沒被移出堆,說(shuō)明這個(gè)最高的矩形還沒結(jié)束。對(duì)于左頂點(diǎn),我們將其加入堆中。 The Skyline Problem A citys skyline is the outer contour of the silhouette formed by all the buildings in that city w...
摘要:建立動(dòng)規(guī)數(shù)組,表示從起點(diǎn)處到達(dá)該點(diǎn)的可能性。循環(huán)結(jié)束后,數(shù)組對(duì)所有點(diǎn)作為終點(diǎn)的可能性都進(jìn)行了賦值。和的不同在于找到最少的步數(shù)。此時(shí)的一定是滿足條件的最小的,所以一定是最優(yōu)解。 Jump Game Problem Given an array of non-negative integers, you are initially positioned at the first index...
摘要:逐位看官,這兩種解法一看便知,小子便不多費(fèi)唇舌了。字符數(shù)組比較法原數(shù)翻轉(zhuǎn)比較法 Problem Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints:Could negative integers be palindrom...
摘要:和方法一樣,多一個(gè)數(shù),故多一層循環(huán)。完全一致,不再贅述, 4Sum Problem Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which ...
摘要:窗口前進(jìn),刪隊(duì)首元素保證隊(duì)列降序加入當(dāng)前元素下標(biāo)從開始,每一次循環(huán)都將隊(duì)首元素加入結(jié)果數(shù)組 Sliding Window Maximum Problem Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration fro...
閱讀 2436·2021-10-09 09:44
閱讀 3792·2021-09-22 15:43
閱讀 2925·2021-09-02 09:47
閱讀 2539·2021-08-12 13:29
閱讀 3871·2019-08-30 15:43
閱讀 1680·2019-08-30 13:06
閱讀 2190·2019-08-29 16:07
閱讀 2745·2019-08-29 15:23