// Divide and Conquer. vector<vector<int>> ComputeSkylineInInterval(const vector<vector<int>>& buildings, int left_endpoint, int right_endpoint) { if (right_endpoint - left_endpoint <= 1) { // 0 or 1 skyline, just copy it. return {buildings.cbegin() + left_endpoint, buildings.cbegin() + right_endpoint}; } int mid = left_endpoint + ((right_endpoint - left_endpoint) / 2); auto left_skyline = ComputeSkylineInInterval(buildings, left_endpoint, mid); auto right_skyline = ComputeSkylineInInterval(buildings, mid, right_endpoint); return MergeSkylines(left_skyline, right_skyline); }
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { const auto intervals = ComputeSkylineInInterval(buildings, 0, buildings.size()); vector<pair<int, int>> res; int last_end = -1; for (const auto& interval : intervals) { if (last_end != -1 && last_end < interval[start]) { res.emplace_back(last_end, 0); } res.emplace_back(interval[start], interval[height]); last_end = interval[end]; } if (last_end != -1) { res.emplace_back(last_end, 0); } return res; }
/** * @param buildings: A list of lists of integers * @return: Find the outline of those buildings */ vector<vector<int>> buildingOutline(vector<vector<int>> &buildings) { return ComputeSkylineInInterval(buildings, 0, buildings.size()); }