摘要:复杂度思路利用的思想,先分成左右两部分,再进行。每次都要将的左上角和右下角推进,进行计算。观察左边和右边进行。
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] ] .
复杂度
O(NlgN), O(N)
思路
利用merge sort的思想,先分成左右两部分,再进行merge。每次都要将building的左上角和右下角推进priorityQueue,进行计算。观察左边和右边进行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; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66137.html
摘要:遍历时,通过一个堆来得知当前图形的最高位置。堆顶是所有顶点中最高的点,只要这个点没被移出堆,说明这个最高的矩形还没结束。对于左顶点,我们将其加入堆中。 The Skyline Problem A citys skyline is the outer contour of the silhouette formed by all the buildings in that city w...
摘要:建立动规数组,表示从起点处到达该点的可能性。循环结束后,数组对所有点作为终点的可能性都进行了赋值。和的不同在于找到最少的步数。此时的一定是满足条件的最小的,所以一定是最优解。 Jump Game Problem Given an array of non-negative integers, you are initially positioned at the first index...
摘要:逐位看官,这两种解法一看便知,小子便不多费唇舌了。字符数组比较法原数翻转比较法 Problem Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints:Could negative integers be palindrom...
摘要:和方法一样,多一个数,故多一层循环。完全一致,不再赘述, 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 ...
摘要:窗口前进,删队首元素保证队列降序加入当前元素下标从开始,每一次循环都将队首元素加入结果数组 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...
阅读 2416·2021-10-09 09:44
阅读 3758·2021-09-22 15:43
阅读 2907·2021-09-02 09:47
阅读 2515·2021-08-12 13:29
阅读 3840·2019-08-30 15:43
阅读 1665·2019-08-30 13:06
阅读 2165·2019-08-29 16:07
阅读 2727·2019-08-29 15:23