From 0cd785e94a3509f9c40bb9e3592af42ae8e0c83b Mon Sep 17 00:00:00 2001 From: Akshat5698 <131651858+Akshat5698@users.noreply.github.com> Date: Fri, 24 Oct 2025 13:51:00 +0530 Subject: [PATCH] Create The Skyline Problem --- The Skyline Problem | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 The Skyline Problem diff --git a/The Skyline Problem b/The Skyline Problem new file mode 100644 index 000000000..596bd4a83 --- /dev/null +++ b/The Skyline Problem @@ -0,0 +1,36 @@ +class Solution { +public: + vector> getSkyline(vector>& buildings) { + int edge_idx = 0; + vector> edges; + priority_queue> pq; + vector> skyline; + + for (int i = 0; i < buildings.size(); ++i) { + const auto &b = buildings[i]; + edges.emplace_back(b[0], i); + edges.emplace_back(b[1], i); + } + + std::sort(edges.begin(), edges.end()); + + while (edge_idx < edges.size()) { + int curr_height; + const auto &[curr_x, _] = edges[edge_idx]; + while (edge_idx < edges.size() && + curr_x == edges[edge_idx].first) { + const auto &[_, building_idx] = edges[edge_idx]; + const auto &b = buildings[building_idx]; + if (b[0] == curr_x) + pq.emplace(b[2], b[1]); + ++edge_idx; + } + while (!pq.empty() && pq.top().second <= curr_x) + pq.pop(); + curr_height = pq.empty() ? 0 : pq.top().first; + if (skyline.empty() || skyline.back()[1] != curr_height) + skyline.push_back({curr_x, curr_height}); + } + return skyline; + } +};