Advertisement
Valkyrie006

Untitled

Oct 20th, 2021
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. // https://leetcode.com/problems/the-skyline-problem/discuss/61222/17-Line-O(n-log-n)-time-O(n)-space-C%2B%2B-Accepted-Easy-Solution-w-Explanations
  2.  
  3. // Idea - first we see max height among current, then when that max height gets over then we go to next max height, and so on....
  4. class Solution {
  5. public:
  6.     vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
  7.         vector<pair<int, int>> heights;
  8.         for(auto &building: buildings) {
  9.             heights.push_back({building[0], -building[2]});
  10.             heights.push_back({building[1], building[2]});
  11.         }
  12.         sort(heights.begin(), heights.end());
  13.         multiset<int> currentHeights;
  14.         currentHeights.insert(0);
  15.         vector<vector<int>> skyline;
  16.         for(auto &[x, y]: heights) {
  17.             if(y < 0) {
  18.                 currentHeights.insert(-y);
  19.             }
  20.             else {
  21.                 currentHeights.erase(currentHeights.find(y));
  22.             }
  23.             if(skyline.empty() || skyline.back()[1] != *currentHeights.rbegin()) {
  24.                 skyline.push_back({x, *currentHeights.rbegin()});
  25.             }
  26.         }
  27.         return skyline;
  28.     }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement