Ref
[1] http://www.mitbbs.com/article_t1/JobHunting/32952623_0_1.html
Question
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).

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:
[0, 10000].Li.[...[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], ...]//C++: method1 TLE class Solution { struct building{ int l; int r; int h; bool operator<(const building& rhs) const{ return r>rhs.r; } building(int l1, int r1, int h1):l(l1),r(r1),h(h1){} }; public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { priority_queue<building> queue_b; priority_queue<int> queue_h; int c_queue = 0; vector<pair<int, int>> res; for(int i=0; i<buildings.size(); ++i){ building b(buildings[i][0], buildings[i][1], buildings[i][2]); while(c_queue && queue_b.top().r<b.l) remove_buildings(queue_b, queue_h, res, c_queue); if(c_queue==0 || b.h>queue_h.top()){ res.push_back(pair<int, int>(b.l, b.h)); } queue_b.push(b); queue_h.push(b.h); c_queue++; } while(c_queue){ remove_buildings(queue_b, queue_h, res, c_queue); } return res; } void remove_buildings(priority_queue<building> &queue_b, priority_queue<int> &queue_h, vector<pair<int, int>> &res, int &c_queue){ building rb = queue_b.top(); queue_b.pop(); queue_h = update_queue_h(queue_b); if(queue_h.empty() || rb.h>queue_h.top()){ res.push_back(pair<int, int>(rb.r, queue_h.empty()?0:queue_h.top())); } c_queue--; } priority_queue<int> update_queue_h(priority_queue<building> queue_b){ priority_queue<int> queue_h; priority_queue<building> queue_b_t = queue_b; while(!queue_b_t.empty()){ queue_h.push(queue_b_t.top().h); queue_b_t.pop(); } return queue_h; } };
//C++: 836ms class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { vector<pair<int, int>> res; priority_queue<pair<int, int>> que; int X, Y, i = 0, n = buildings.size(); while(i<n || !que.empty()){ if(!que.empty() && (i==n || que.top().second<buildings[i][0])){ X = que.top().second; while(!que.empty() && que.top().second<=X){ que.pop(); } }else if(i<n){ X = buildings[i][0]; while(i<n && buildings[i][0]==X){ que.push(pair<int, int>(buildings[i][2], buildings[i][1])); i++; } } Y = que.empty()?0:que.top().first; if(res.empty() || Y!=res.back().second){ res.push_back(pair<int, int>(X, Y)); } } return res; } };
//Java class Pair{ int height, right; Pair(int h, int r){ height = h; right = r; } public String toString(){ return height+" "+right; } }; //ordered first in height, then in right. class PairComp implements Comparator<Pair>{ public int compare(Pair p1, Pair p2){ if(p1.height<p2.height) return 1; else if(p1.height==p2.height){ if(p1.right<p2.right) return 1; if(p1.right==p2.right) return 0; else return -1; }else return -1; } }; /* A point needs to be recorded in 3 cases: 1. For a building B, if its top left conner is not overlapped, record it. 2. otherwise, it must be overlapped by a taller(or equal) building T. In this case, record <T.R, Y>, unless Y is not changed compared to the last record point. 3. If B's bottom right conner is exposed, record it. Create a priorityqueue(pq) to maintain the buildings. A building B is added to pq if it overlaps with the tallest building T in pq. Buildings in pq are sorted in descending order is height first, and then in right. */ public class Solution { public List<List<Integer>> getSkyline(int[][] buildings) { List<List<Integer>> ret = new ArrayList<>(); PriorityQueue<Pair> pq = new PriorityQueue<Pair>(new PairComp()); int i = 0, n = buildings.length; int X = 0, Y = 0; while(i<n || !pq.isEmpty()){ if(!pq.isEmpty() && (i==n || pq.peek().right<buildings[i][0])){ X = pq.peek().right; while(!pq.isEmpty()&&pq.peek().right<=X){ pq.poll(); } }else if(i<n){ X = buildings[i][0]; while(i<n && buildings[i][0]==X){ pq.add(new Pair(buildings[i][2], buildings[i][1])); i++; } } Y = pq.isEmpty() ? 0 : pq.peek().height; if(ret.isEmpty() || Y!=ret.get(ret.size()-1).get(1)) ret.add(Arrays.asList(new Integer[]{Integer.valueOf(X), Integer.valueOf(Y)})); } return ret; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class Solution { public List<List<Integer>> getSkyline(int[][] buildings) { //height, right: high to low then right to left PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> { if(a[0]==b[0]) return b[1]-a[1]; else return b[0]-a[0]; }); int i = 0, n = buildings.length; int X = 0, Y = 0; List<List<Integer>> ret = new ArrayList<>(); while(!pq.isEmpty()||i<n){ if(!pq.isEmpty() && (i==n || pq.peek()[1]<buildings[i][0])){ X = pq.peek()[1]; while(!pq.isEmpty() && pq.peek()[1]<=X) pq.poll(); }else{ X = buildings[i][0]; while(i<n && buildings[i][0]==X){ pq.add(new int[]{buildings[i][2], buildings[i][1]}); i++; } } Y = pq.isEmpty()?0:pq.peek()[0]; if(ret.isEmpty() || Y!=ret.get(ret.size()-1).get(1)){ ret.add(Arrays.asList(new Integer[]{X, Y})); } } return ret; } } |
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Solution { public: int findKthLargest(vector<int>& nums, int k) { priority_queue<int> pq; for(auto n:nums){ pq.push(-n); if(pq.size()>k) pq.pop(); } return -pq.top(); } }; class Solution_QS { int partition(vector<int>& nums, int l, int h){ int pivot = nums[h]; int i = l-1; for(int j=l; j<h; ++j){ if(nums[j]<=pivot){ swap(nums[++i], nums[j]); } } swap(nums[++i], nums[h]); return i; } int select(vector<int>& nums, int l, int h, int k){ if(l>h) return 0; int p = partition(nums, l, h); if(p==k) return nums[p]; if(p<k) return select(nums, p+1, h, k); else return select(nums, l, p-1, k); } public: int findKthLargest(vector<int>& nums, int k) { int n = nums.size(); k = n-k;//invert to incremental order return select(nums, 0, nums.size()-1, k); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | //Java, Quick Sort class Solution { void swap(int[] nums, int i, int j){ int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } int partition(int[] nums, int l, int r){ int pivot = nums[r]; int i = l; for(int j=l; j<r; ++j){ if(nums[j]<=pivot){ swap(nums, i, j); i++; } } swap(nums, i, r); return i; } int select(int[] nums, int l, int r, int k){ if(l>r) return 0; int p = partition(nums, l, r); if(p==k) return nums[p]; if(p<k) return select(nums, p+1, r, k); else return select(nums, l, p-1, k); } public int findKthLargest(int[] nums, int k) { return select(nums, 0, nums.length-1, nums.length-k); } } |
Input: [ 1->4->5, 1->3->4, 2->6 ] Output: 1->1->2->3->4->4->5->6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ struct Comp { bool operator()(ListNode *a, ListNode *b) { return a->val > b->val; } }; class Solution { public: ListNode *mergeKLists(vector<ListNode *> &lists) { priority_queue<ListNode *, vector<ListNode *>, Comp> pq; for (int i = 0; i < lists.size(); ++i) { if(lists[i]) pq.push(lists[i]); } ListNode *Head = new ListNode(0); ListNode *p = Head; while(!pq.empty()) { ListNode *l = pq.top(); pq.pop(); p->next = l; p = p->next; if(l->next) pq.push(l->next); } return Head->next; } }; class Solution { ListNode *divide(vector<listnode *> &lists, int a, int b){ if(a>b) return NULL; if(a==b) return lists[a]; int m=(a+b)/2; ListNode *la = divide(lists, a, m); ListNode *lb = divide(lists, m+1, b); return merge(la, lb); } ListNode *merge(ListNode *la, ListNode *lb){ ListNode *sentinel = new ListNode(0); ListNode *p = sentinel; while(la && lb){ if(la->val<=lb->val){ p->next = la; la = la->next; }else{ p->next = lb; lb = lb->next; } p = p->next; } if(la) p->next = la; if(lb) p->next = lb; return sentinel->next; } public: ListNode *mergeKLists(vector<listnode *> &lists) { ListNode * sentinel = NULL; return divide(lists, 0, lists.size()-1); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode mergeKLists(ListNode[] lists) { ListNode H = new ListNode(); ListNode p = H; int n = lists.length; PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b)->a.val-b.val); for(ListNode node : lists) if(node!=null) pq.add(node); while(!pq.isEmpty()){ ListNode node = pq.poll(); p.next = node; p = p.next; if(node.next!=null) pq.add(node.next); } return H.next; } } |