[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

 

目录
相关文章
Leetcode 365. Water and Jug Problem
一句话理解题意:有容积为x和y升的俩水壶,能不能量出z升的水。 我刚开始看到这题,立马就想了下暴力搜索的可能性,但考虑了下数据大小,立马放弃这个暴力的想法,于是意识到肯定有比较简单的数学方法,其实我自己没想到,后来看还是看了别人的代码,很多博客都直接给出了解法, 但没介绍为什么能这么解。所以我决定解释下我自己的思路。
58 0
|
4月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
5月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
65 6
|
5月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
133 2
|
2月前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
57 1
|
4月前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
5月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
80 7