|
| 1 | +logo |
| 2 | + |
| 3 | +218. The Skyline Problem |
| 4 | +Description |
| 5 | +Hints |
| 6 | +Submissions |
| 7 | +Discuss |
| 8 | +Solution |
| 9 | + |
| 10 | +C++ T:O(nlogn)/S:O(n) with vector/multiset |
| 11 | +C++ T:O(nlogn)/S:O(n) with vector/multiset |
| 12 | +311 |
| 13 | +VIEWS |
| 14 | +4 |
| 15 | +Created at: November 30, 2020 12:00 PM |
| 16 | + |
| 17 | +HalleyWang |
| 18 | +HalleyWang |
| 19 | + 59 |
| 20 | +/* |
| 21 | + sort x coord first, we will handle each point in x-increasing order. |
| 22 | + For each building, we will have 2 points in our list, (L,H), (R,-H). Negative height means out of the building's width. |
| 23 | + Maintain a priority queue to find out the current max y coord. |
| 24 | + T:O(nlogn)/S:O(n) |
| 25 | +*/ |
| 26 | +class Solution { |
| 27 | +public: |
| 28 | + vector<vector<int>> getSkyline(vector<vector<int>>& buildings) |
| 29 | + { |
| 30 | + vector<pair<int,int>> s; |
| 31 | + multiset<int> q; |
| 32 | + vector<vector<int>> ans; |
| 33 | + for (auto building:buildings) { |
| 34 | + s.push_back({building[0],building[2]}); |
| 35 | + s.push_back({building[1],-1*building[2]}); |
| 36 | + } |
| 37 | + sort(s.begin(),s.end(),[](auto a, auto b){ |
| 38 | + if (a.first==b.first) return a.second>=b.second; |
| 39 | + else return a.first<=b.first; |
| 40 | + }); |
| 41 | + q.insert(0); int cur_max=0; |
| 42 | + for (auto building:s) { |
| 43 | + if (building.second<0) |
| 44 | + q.erase(q.find(-1*building.second)); |
| 45 | + else q.insert(building.second); |
| 46 | + if (*(q.rbegin())!=cur_max) { |
| 47 | + cur_max=*(q.rbegin()); |
| 48 | + ans.push_back({building.first,cur_max}); |
| 49 | + } |
| 50 | + } |
| 51 | + return ans; |
| 52 | + } |
| 53 | +}; |
0 commit comments