博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] The Skyline Problem
阅读量:5077 次
发布时间:2019-06-12

本文共 2710 字,大约阅读时间需要 9 分钟。

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: , . Of course, there is still a divide-and-conquer solution on , 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
> getSkyline(vector
>& buildings) { 4 int idx = 0, start, height, n = buildings.size(); 5 vector
> skyline; 6 priority_queue
> 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 += 113             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

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4644209.html

你可能感兴趣的文章
Java跟Javac,package与import
查看>>
day-12 python实现简单线性回归和多元线性回归算法
查看>>
Json格式的字符串转换为正常显示的日期格式
查看>>
[转]使用 Razor 进行递归操作
查看>>
[转]Android xxx is not translated in yyy, zzz 的解决方法
查看>>
docker入门
查看>>
Android系统--输入系统(十一)Reader线程_简单处理
查看>>
监督学习模型分类 生成模型vs判别模型 概率模型vs非概率模型 参数模型vs非参数模型...
查看>>
Mobiscroll脚本破解,去除Trial和注册时间限制【转】
查看>>
实验五 Java网络编程及安全
查看>>
32位与64位 兼容编程
查看>>
iframe父子页面通信
查看>>
ambari 大数据安装利器
查看>>
java 上传图片压缩图片
查看>>
magento 自定义订单前缀或订单起始编号
查看>>
ACM_拼接数字
查看>>
计算机基础作业1
查看>>
Ubuntu 深度炼丹环境配置
查看>>
C#中集合ArrayList与Hashtable的使用
查看>>
从一个标准 url 里取出文件的扩展名
查看>>