[LeetCode] The Skyline Problem

An interesting problem! But not very easy at first glance. You need to think very clear about how will a keypoint be generated. Since I only learn from others‘ solutions and am still unable to give my personal explanations, I would suggest you to these solutions: C++ priority_queue solution, Python heapq solution. Of course, there is still a divide-and-conquer solution on Geeksforgeeks.com, which is much more difficult to understand :-)

Well, I just rewrite the code in the first solution as follows, by giving those variables more meaning names to help understand it.

 1 class Solution {
 2 public:
 3     vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
 4         int idx = 0, start, height, n = buildings.size();
 5         vector<pair<int, int> > skyline;
 6         priority_queue<pair<int, int> > liveBuildings;
 7         while (idx < n || !liveBuildings.empty()) {
 8             if (liveBuildings.empty() || idx < n && buildings[idx][0] <= liveBuildings.top().second) {
 9                 start = buildings[idx][0];
10                 while (idx < n && buildings[idx][0] == start) {
11                     liveBuildings.push(make_pair(buildings[idx][2], buildings[idx][1]));
12                     idx++;
13                 }
14             }
15             else {
16                 start = liveBuildings.top().second;
17                 while (!liveBuildings.empty() && liveBuildings.top().second <= start)
18                     liveBuildings.pop();
19             }
20             height = liveBuildings.empty() ? 0 : liveBuildings.top().first;
21             if (skyline.empty() || skyline.back().second != height)
22                 skyline.push_back(make_pair(start, height));
23         }
24         return skyline;
25     }
26 };
时间: 2024-08-12 00:17:16

[LeetCode] The Skyline Problem的相关文章

[LeetCode] The Skyline Problem 天际线问题

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a pr

解题报告:LeetCode The Skyline Problem(画天际

题目出处:https://leetcode.com/submissions/detail/47013144/题意描述: 给定一系列矩形的左边坐标Li,右边坐标Ri,和高度Hi(其中Li按照从小到大的顺序排列).代表城市中一座座高楼.求这些矩形代表的高楼行成的天际线.天际线的定义为:在远处看这些所有的高楼时看到的轮廓. 数据输入: [ [L1, R1, H1], [Li, Ri, Hi]...]为元素类型为三维向量的向量,其中Li,Ri,Hi的含义见题意描述.且输入数据保证: 0 ≤ Li, Ri

[LeetCode-JAVA] The Skyline Problem

题目:题目太长了,见链接-- > The Skyline Problem Notes: The number of buildings in any input list is guaranteed to be in the range [0, 10000]. The input list is already sorted in ascending order by the left x position Li. The output list must be sorted by the x

[LeetCode#218] The Skyline Problem

Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), w

LeetCode题218——The Skyline Problem

https://leetcode.com/problems/the-skyline-problem/description/ A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the

Java for LeetCode 218 The Skyline Problem【Comming Soon】

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a pr

The Skyline Problem leetcode 详解

class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { vector<pair<int, int> > h, res; multiset<int> m; int pre = 0, cur = 0; for (auto &a : buildings) { h.push_back({

The Skyline Problem

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a pr

Leetcode-The Skyline Problem题解

一. 题目 Leetcode平台上天际线问题 A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo