[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.

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 };

The Python translation is as follows. Note that since the default heapq is a min heap, we take the negative of the left and right positions and the height of buildings.

 1 class Solution:
 2     # @param {integer[][]} buildings
 3     # @return {integer[][]}
 4     def getSkyline(self, buildings):
 5         idx, n = 0, len(buildings)
 6         liveBuildings, skyline = [], []
 7         while idx < n or len(liveBuildings) > 0:
 8             if len(liveBuildings) == 0 or (idx < n and buildings[idx][0] <= -liveBuildings[0][1]):
 9                 start = buildings[idx][0]
10                 while idx < n and buildings[idx][0] == start:
11                     heapq.heappush(liveBuildings, [-buildings[idx][2], -buildings[idx][1]])
12                     idx += 1
13             else:
14                 start = -liveBuildings[0][1]
15                 while len(liveBuildings) > 0 and -liveBuildings[0][1] <= start:
16                     heapq.heappop(liveBuildings)
17             height = len(liveBuildings) and -liveBuildings[0][0]
18             if len(skyline) == 0 or skyline[-1][1] != height:
19                 skyline.append([start, height])
20         return skyline

 

目录
相关文章
|
6天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
6天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
13 0
|
6天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
25 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
6天前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
27 5
【刷题】 leetcode 面试题 01.06 字符串压缩
|
6天前
|
存储 算法 测试技术
|
6天前
|
算法 C语言 C++