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

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

class Edge {	int x;	int height;	boolean isStart; 	public Edge(int x, int height, boolean isStart) {		this.x = x;		this.height = height;		this.isStart = isStart;	}}public List
getSkyline(int[][] buildings) { List
result = new ArrayList
(); if (buildings == null || buildings.length == 0 || buildings[0].length == 0) { return result; } List
edges = new ArrayList
(); // add all left/right edges for (int[] building : buildings) { Edge startEdge = new Edge(building[0], building[2], true); edges.add(startEdge); Edge endEdge = new Edge(building[1], building[2], false); edges.add(endEdge); } // sort edges Collections.sort(edges, new Comparator
() { public int compare(Edge a, Edge b) { if (a.x != b.x) return Integer.compare(a.x, b.x); if (a.isStart && b.isStart) { return Integer.compare(b.height, a.height); } if (!a.isStart && !b.isStart) { return Integer.compare(a.height, b.height); } return a.isStart ? -1 : 1; } }); // process edges PriorityQueue
heightHeap = new PriorityQueue
(10, Collections.reverseOrder()); for (Edge edge : edges) { if (edge.isStart) { if (heightHeap.isEmpty() || edge.height > heightHeap.peek()) { result.add(new int[] { edge.x, edge.height }); } heightHeap.add(edge.height); } else { heightHeap.remove(edge.height); if(heightHeap.isEmpty()){ result.add(new int[] {edge.x, 0}); }else if(edge.height > heightHeap.peek()){ result.add(new int[]{edge.x, heightHeap.peek()}); } } } return result;}

 

转载于:https://www.cnblogs.com/zmyvszk/p/5874449.html

你可能感兴趣的文章
floyd求最小环 模板
查看>>
SqlServer索引的原理与应用
查看>>
使用Kubeadm搭建Kubernetes(1.12.2)集群
查看>>
微信小程序获取当前地址以及选择地址详解 地点标记
查看>>
任务平均分配的小算法
查看>>
学习日报 7-10(验证码)
查看>>
No.3 - CSS transition 和 CSS transform 配合制作动画
查看>>
c++STL全排列
查看>>
MYSQL 5.1自动安装脚本
查看>>
Sql Server数据库备份和恢复:原理篇
查看>>
Git学习脑图
查看>>
fafu oj 1266 数数
查看>>
日期和时间模块
查看>>
开发系列:03、Spark Streaming Custom Receivers(译)
查看>>
fixed与sticky的区别
查看>>
keil C51 例子
查看>>
修改hosts文件在本地使域名解析到指定IP
查看>>
python数字图像处理(5):图像的绘制
查看>>
Gym 101147J Whistle's New Car(dfs)
查看>>
poj 3669 Meteor Shower
查看>>