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

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

package com.chanmufeng.questions;import java.util.*;public class Skyline {    public static class Node {        public int pos;        public int height;        public boolean isUp;        public Node(int pos, int height, boolean isUp) {            this.pos = pos;            this.height = height;            this.isUp = isUp;        }        @Override        public String toString() {            return "[" + pos + " " + height + " " + isUp + "]";        }    }    public static class NodeComparator implements Comparator
{ @Override public int compare(Node o1, Node o2) { //如果位置不相等,将位置升序 if (o1.pos != o2.pos) { return o1.pos - o2.pos; } //如果位置相等,优先排列下降的位置 if (o1.isUp != o2.isUp) { return o1.isUp ? 1 : -1; } return 0; } } public static List
getSkyline(int[][] buildings) { Node[] nodes = new Node[buildings.length * 2]; int index = 0; for (int i = 0; i < buildings.length; i++) { nodes[index++] = new Node(buildings[i][0], buildings[i][2], true); nodes[index++] = new Node(buildings[i][1], buildings[i][2], false); } Arrays.sort(nodes, new NodeComparator()); //key:高度 value:高度出现的次数 TreeMap
htMap = new TreeMap<>(); //key:position value:该位置的最高高度 TreeMap
pmMap = new TreeMap<>(); List
res = new LinkedList<>(); for (int i = 0; i < nodes.length; i++) { if (nodes[i].isUp) { if (!htMap.containsKey(nodes[i].height)) { htMap.put(nodes[i].height, 1); } else { htMap.put(nodes[i].height, htMap.get(nodes[i].height) + 1); } } else { if (htMap.get(nodes[i].height) == 1) { htMap.remove(nodes[i].height); } else { htMap.put(nodes[i].height, htMap.get(nodes[i].height) - 1); } } if (htMap.isEmpty()) { pmMap.put(nodes[i].pos, 0); } else { pmMap.put(nodes[i].pos, htMap.lastKey()); } } int height = 0; Iterator it = pmMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); int curMaxHeight = (int) entry.getValue(); if (height != curMaxHeight) { int[] temp = new int[2]; temp[0] = (int) entry.getKey(); temp[1] = curMaxHeight; ((LinkedList
) res).addLast(temp); height = curMaxHeight; } } return res; } public static void main(String[] args) { int[][] arr = { {2, 9, 10}, {3, 7, 15}, {5, 12, 12}, {15, 20, 10}, {19, 24, 8}, };// int[][] arr = {// {1, 3, 3},// {2, 4, 2},// {4, 5, 1},// }; List res = getSkyline(arr); for (int i = 0; i < res.size(); i++) { for (int item : (int[]) res.get(i)) { System.out.print(item + " "); } System.out.println(); } }}

转载地址:http://ksaxb.baihongyu.com/

你可能感兴趣的文章
js页面跳转整理
查看>>
在64位Win7操作系统中安装Microsoft Access Engine的解决方案
查看>>
30类CSS选择器
查看>>
微信支付的使用介绍
查看>>
PHP单例模式应用详解
查看>>
冒号课堂§5.2:数据类型
查看>>
博客搬家
查看>>
冒号课堂§6.2:平台语言
查看>>
《关于信息系统组织方式的一个提案》的评论与反评
查看>>
冒号和他的学生们(连载10)——超级范式
查看>>
冒号和他的学生们(连载9)——泛型范式
查看>>
冒号和他的学生们(连载13)——范式总结
查看>>
A Proposal on Organization of Information System
查看>>
冒号和他的学生们(连载2)——首轮提问
查看>>
正则表达式与文件格式化处理
查看>>
Java EE互联网轻量级框架整合开发
查看>>
Java语言程序设计(基础篇)
查看>>
大型网站技术架构:核心原理与案例分析
查看>>
JAVA并发编程实战
查看>>
RabbitMQ实战++高效部署分布式消息队列
查看>>