Advertisement
nikunjsoni

218

May 22nd, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
  4.         vector<pair<int, int>> heights;
  5.         for(auto b: buildings){
  6.             heights.push_back({b[0], -b[2]});
  7.             heights.push_back({b[1], b[2]});
  8.         }
  9.         sort(heights.begin(), heights.end());
  10.        
  11.         multiset<int> pq;
  12.         pq.insert(0);
  13.         int curMax = 0;
  14.         vector<vector<int>> ans;
  15.         for(auto height: heights){
  16.             int h = height.second;
  17.             if(h<0)
  18.                 pq.insert(-h);
  19.             else
  20.                 pq.erase(pq.find(h));
  21.             if(curMax != *pq.rbegin()){
  22.                 curMax = *pq.rbegin();
  23.                 ans.push_back({height.first, curMax});
  24.             }
  25.         }
  26.         return ans;
  27.     }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement