LeetCode 218. The Skyline Problem(java)

【信息科学与工程学】【数据科学】数据科学领域 第六篇 算法设计 07 堆、栈、表、矩阵、集合、队列、数组、字符串、树、图、动态规划、结构体、元组的算法01 当前段必须包含所有字符的最后出现位置。1. 字符串分割成最多片段2. 基因序列功能域划分3. 文本段落划分4. 音频声道分离5. 图像色彩分区6. 社交网络社群划分7. 蛋白质结构域8. 音乐乐章分段9. 代码模块划分10. 章节划分特征:一次扫描,贪心确定边界。 阅读详情

A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
这里写图片描述
Buildings Skyline Contour
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .

The output is a list of “key points” (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], … ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].

Notes:

The number of buildings in any input list is guaranteed to be in the range [0, 10000].
The input list is already sorted in ascending order by the left x position Li.
The output list must be sorted by the x position.
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, […[2 3], [4 5], [7 5], [11 5], [12 7]…] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: […[2 3], [4 5], [12 7], …]

思路:类似于meeting room的方法,用hashmap存《start, +1》和《end, -1》。在这里就是先用一个List《int[]》来存储《start, height》和《end, -height》, 然后把这个list按照从小到大排序,最后用一个queue来走一遍每个点,每次遇到正值的height,就offer,遇到负值就pop此值,然后在每一个点,都peek heap中的最大height,如果这个height和之前存在result里的值不同,将此点和值存进result,否则更新pre,继续向后走,直到走完。
代码:
public List<int[]> getSkyline(int[][] buildings) {
        List<int[]> list = new ArrayList<>();
        if (buildings.length == 0 || buildings[0].length == 0) return list;
        List<int[]> height = new ArrayList<>();
        for (int i = 0; i < buildings.length; i++) {
            height.add(new int[]{buildings[i][0], buildings[i][2]});
            height.add(new int[]{buildings[i][1], -buildings[i][2]});
        }
        Collections.sort(height, new Comparator<int[]>(){
           public int compare(int[] o1, int[] o2) {
           //注意这里是O2[1] - o1[0]!
               if (o1[0] == o2[0]) return o2[1] - o1[1];
               else return o1[0] - o2[0];
           } 
        });
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        queue.offer(0);
        int cur = 0, pre = 0;
        for (int[] h : height) {
            if (h[1] > 0) {
                queue.offer(h[1]);
            } else {
                queue.remove(-h[1]);
            }
            cur = queue.peek();
            if (pre != cur) {
                list.add(new int[]{h[0], cur});
                pre = cur;
            }
        }
        return list;
    }
从Mule ESB到Apache Camel:手把手教你设计一个轻量级企业集成框架的‘心脏’ 本文深入探讨了从Mule ESB到Apache Camel的轻量级企业集成框架设计实践,对比了两者在协议支持、消息处理能力和部署灵活性等方面的差异。通过实际代码示例和性能优化技巧,帮助开发者构建高效的核心模块,适用于现代微服务架构下的系统集成需求。 阅读详情

相关推荐

浮点数运算的陷阱与启示:FP16加法器/乘法器开发中的常见误区

本文深入探讨FP16加法器和乘法器开发中的常见误区,包括FP16格式解析、阶码对齐陷阱、尾数运算风险及规格化处理。通过Verilog实战代码展示防御性编程技巧,帮助开发者避免边界条件处理和异常状态管理中的错误,提升硬件设计的可靠性和效率。

dell8的博客 257

leetcode-218天际线问题

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Mar

qq_40448796的博客 364

Android SDK安装教程(超详细),从零基础入门到精通,从这篇开始_android sdk下载安装

Android SDK安装教程(超详细),从零基础入门到精通,从这篇开始_android sdk下载安装

Jinmindong 5045

LeetCode——218. 天际线问题(The Skyline Problem)[困难]——分析及代码(Java)

LeetCode——218. 天际线问题[The Skyline Problem][困难]——分析及代码[Java]一、题目二、分析及代码1. 扫描线 + 优先队列(1)思路(2)代码(3)结果三、其他 一、题目 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。给你所有建筑物的位置和高度,请返回由这些建筑物形成的 天际线 。 每个建筑物的几何信息由数组 buildings 表示,其中三元组 buildings[i] = [lefti, righti, heighti] 表示: lefti

江南土豆的博客 1891

STM32驱动MG996R舵机的PWM控制与闭环原理

舵机是一种典型的模拟闭环伺服系统,其核心是通过电位器反馈实现角度位置的自动校正。工作原理基于负反馈控制:目标PWM脉宽决定期望角度,电位器输出实时反馈实际角度,控制电路计算偏差并驱动电机消除误差。该机制赋予舵机强带载能力与抗干扰性,是机器人、云台、智能执行器等场景的关键执行单元。在嵌入式开发中,STM32通过定时器生成标准20ms周期、0.5–2.5ms脉宽的PWM信号,需严格遵循电气规范,并解决PA15引脚SWJ复用冲突、定时器重映射及CCR精度计算等关键问题。本文深入解析MG996R舵机控制全流程,涵盖

weixin_42600407的博客 574

Python高效转换病理图像:从mrxs到SVS的完整实践指南

本文提供了使用Python将数字病理切片从mrxs格式高效转换为SVS格式的完整实践指南。针对mrxs格式兼容性差的问题,文章详细解析了基于openslide和tifffile库的核心转换流程,并重点分享了应对大内存消耗的优化方案与流式处理技巧,确保转换后的SVS文件保留多分辨率金字塔结构,兼容主流病理分析工具。

weixin_29051811的博客 559

leetcode 218. The Skyline Problem java代码

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as

azs1478963的专栏 941

别再为格式发愁:safetensors转GGUF的三种高效方法及Ollama集成对比

本文详细介绍了将safetensors格式转换为GGUF格式的三种高效方法,包括llama.cpp原生转换、GGUF官方工具链和Transformers中转方案,并对比了它们在Ollama集成中的表现。文章还提供了实际项目中的踩坑经验和性能调优建议,帮助开发者快速掌握模型转换技术。

weixin_42651281的博客 654

【leetcode】218.天际线问题 (经典面试题,多种解法详细解释,java实现)

218. 天际线问题 难度困难256 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。现在,假设您获得了城市风光照片(图A)上显示的所有建筑物的位置和高度,请编写一个程序以输出由这些建筑物形成的天际线(图B)。 每个建筑物的几何信息用三元组 [Li,Ri,Hi] 表示,其中 Li 和 Ri 分别是第 i 座建筑物左右边缘的 x 坐标,Hi 是其高度。可以保证 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX 和 Ri - Li > 0。您可以假设所有

Viper的程序员修炼手册 1213

海康威视 2024届 数字逻辑设计 实习笔试分析

记录一下 5月11日晚,做的海康威视的一场笔试。分享给需要的IC人。 岗位:数字逻辑设计工程师(浙江 杭州) 转载需要本人同意! 我的见解不一定都是准确的,欢迎评论区交流指正~~

记录所学,分享知识,结识挚友 1万+

信号的反射(振铃、台阶、回勾、尖峰毛刺)

反射是怎么形成的 假设信号传输过程中,经过两个阻抗不同的区域,如下图所示。区域1阻抗为Z1,区域2阻抗为Z2。 在分界面处,电压必须是连续的,否则,在分界面处会产生无穷大的电场,这在真实世界中是不可能的。 同样,分界面两侧的电流必须是连续的,否则分界面处会产生无穷大的磁场。 因此: 根据欧姆定律,必然有: 显然如果Z1≠Z2的情况下,上面式子不可能同时成立。如何理解这一困境? 应该从 ...

sinat_15677011的博客 1万+

leetcode 218. The Skyline Problem 使用PriorityQueue的java实现 以及细致分析

题目解析:要得到大致轮廓,我们首先可以将X轴排序,高度为正插入,高度为负,删除对应高度。每次取得一个高度应该是所有高度中最高的,想到使用PriorityQueue。每次插入或删除,需要比较当前高度与之前最高值的大小,不一样就更新结果集。 public List getSkyline(int[][] bs) { List res=new ArrayList();

通往花开的路上 944

网络安全与黑客攻防常见设备:前置机、跳板机、堡垒机与肉鸡

前置机、跳板机、堡垒机和肉鸡,这些看似冷僻的技术名词,实则是网络安全与黑客攻防战中的关键角色。它们有的守护着企业的数字Pltform,保卫网络安全;有的则成为黑客的跳板,游走于灰色地带。

网络技术联盟站 990

LeetCode-218. The Skyline Problem [C++][Java]

A city'sskylineis the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Given the locations and heights of all the buildings, returntheskylineformed by these buildings collectively.

贫道绝缘子的博客 500

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

LeetCode(218):天际线问题 The Skyline Problem(Java)

2019.9.18 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 这道智力题乍一看可以用动态规划处理,楼高从左至右类似一个入栈出栈的过程(但实际并不能用栈,因为出栈的楼高不一定在栈顶),而且每一次使用的“栈顶元素”是当前最高的楼高。那么只要能想到一种数据结构使之满足这种功能,那就可以想出动态规划的思路了。 动态规划 动态规划算法图解 这种数据结构可以是最大堆(删除复杂度...

NJU_ChopinXBP的博客 1020

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

天际线java_LeetCode #218 The Skyline Problem 天际线问题

Description:A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the ...

weixin_42511557的博客 259

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Java for LeetCode 218 The Skyline Problem【HARD】

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as...

weixin_30852451的博客 125

LeetCode Top Interview Questions 218. The Skyline Problem (Java版; Hard)

welcome to my blog LeetCode Top Interview Questions 218. The Skyline Problem (Java版; Hard) 题目描述 A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when vi...

littlehaes的博客 218

LeetCode 218. The Skyline Problem

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as

Index.html 1773

LeetCode218. The Skyline Problem

https://leetcode.com/problems/the-skyline-problem/description/ A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now s...

dbvz4901的博客 191

leetcode 218: The Skyline Problem

题目链接:(https://leetcode.com/problems/the-skyline-problem/)示例代码:public class Solution { public List<int[]> getSkyline(int[][] buildings) { List<int[]> res = new ArrayList<int[]>(); Pr

peterkang202的专栏 887

21.1.28 LeetCode 218.The Skyline Problem

218. The Skyline Problem 题意 一座城市的天际线是从远处看时,该城市所有建筑物形成的轮廓的外部轮廓。给定所有建筑的位置和高度,返回这些建筑共同形成的天际线。 每个建筑物的几何信息在阵列建筑物中给出 其中建筑物[i]=[lefti,righti,heighti]: lefti是第i个建筑左边缘的x坐标。 righti是第i个建筑右边缘的x坐标。 heighti是第i栋楼的高度。 你可以假设所有的建筑都是完美的长方形,坐落在高度为0的绝对平坦的表面上。 天际线应表示为按x坐标排序的“

weixin_51972899的博客 203

leetcode 218. The Skyline Problem 优先级队列PriorityQueue + mulitset模拟Heap

A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as

JackZhangNJU的专栏 662

leetcode-218-天际线问题-java

题目及测试 package pid218; /*天际线问题 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。现在,假设您获得了城市风光照片(图A)上显示的所有建筑物的位置和高度,请编写一个程序以输出由这些建筑物形成的天际线(图B)。 Buildings Skyline Contour https://leetcode-cn.com/problems/the-skyline-problem/ 每个建筑物的几何信息用三元组 [Li,Ri,Hi] 表示,其中 Li 和 Ri 分别是第

今天吃了吗的博客 911

精选leetCode第218题天际线(堆(优先队列)

大家好我是魔笑,今天带给大家的是优先队列,写题的目的是为了让自己更能理解这题。也方便后续回顾,也能分享给对这题比较感兴趣的小伙伴,这道题还是比较值的研究一下的,逻辑还是比较难的,下面就是我对这道题的解答,和理解 题目: 城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。给你所有建筑物的位置和高度,请返回由这些建筑物形成的天际线。 首先看一下例题,这是从leetCode题目截取出来的: 下面每个长方体,代表一个建筑,每个建筑物的几何信息由数组buildings表示,其中三元组b...

weixin_44291453的博客 494
上一篇: LeetCode 204. Count Primes(java)
下一篇: LeetCode 312. Burst Balloons(java)
katrina95
博客等级 码龄9年 6粉丝 229原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值