LeetCode 699. Falling Squares 掉落的方块 (Java)
题目:
On an infinite number line (x-axis), we drop given squares in the order they are given.
The i-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point being positions[i][0] and sidelength positions[i][1].
The square is dropped with the bottom edge parallel to the number line, and from a higher height than all currently landed squares. We wait for each square to stick before dropping the next.
The squares are infinitely sticky on their bottom edge, and will remain fixed to any positive length surface they touch (either the number line or another square). Squares dropped adjacent to each other will not stick together prematurely.
Return a list ans of heights. Each height ans[i] represents the current highest height of any square we have dropped, after dropping squares represented by positions[0], positions[1], ..., positions[i].
Example 1:
Input: [[1, 2], [2, 3], [6, 1]]
Output: [2, 5, 5]
Explanation:
After the first drop of positions[0] = [1, 2]: _aa _aa ------- The maximum height of any square is 2.
After the second drop of positions[1] = [2, 3]: __aaa __aaa __aaa _aa__ _aa__ -------------- The maximum height of any square is 5. The larger square stays on top of the smaller square despite where its center of gravity is, because squares are infinitely sticky on their bottom edge.
After the third drop of positions[1] = [6, 1]: __aaa __aaa __aaa _aa _aa___a -------------- The maximum height of any square is still 5. Thus, we return an answer of [2, 5, 5].
Example 2:
Input: [[100, 100], [200, 100]]
Output: [100, 100]
Explanation: Adjacent squares don't get stuck prematurely - only their bottom edge can stick to surfaces.
Note:
1 <= positions.length <= 1000.1 <= positions[i][0] <= 10^8.1 <= positions[i][1] <= 10^6.
分析:
简单来说这道题就是会在一个区域内掉落一些方块,给定掉落的起点和方块的边长,每掉落一个方块都要求得此时区域内最大的高度。
由于方块之间有可能会叠加起来,例如[2,2],[3,1],这种情况当第二个方块到来之后,由于叠加,最大高度就变成了3.
我们维护一个list,用来保存加入的方块所达到的最大高度,每新加入一个方块,就遍历list,找到和当前加入方块发生重叠的部分,计算重叠部分的最大高度,那么这个最大高度再加上方块的边长就是这个方块的最大高度了,再将这个方块的信息加入到list中,继续掉落方块即可。
程序:
class Solution {
public List<Integer> fallingSquares(int[][] positions) {
List<int[]> list = new ArrayList<>();
List<Integer> res = new ArrayList<>();
int maxHeight = Integer.MIN_VALUE;
for(int[] arr:positions){
int start = arr[0];
int end = arr[0] + arr[1];
//int[] interval = new int[]{arr[0], arr[0]+arr[1], arr[1]}; //{start, end, height}
int tempHeight = 0;
for(int[] interval:list){
if(end <= interval[0] || start >= interval[1])
continue;
tempHeight = Math.max(tempHeight, interval[2]);
}
int height = tempHeight + arr[1];
list.add(new int[]{start, end, height});
maxHeight = Math.max(maxHeight, height);
res.add(maxHeight);
}
return res;
}
}
LeetCode 699. Falling Squares 掉落的方块 (Java)的更多相关文章
- leetcode 699. Falling Squares 线段树的实现
线段树实现.很多细节值得品味 都在注释里面了 class SegTree: def __init__(self,N,query_fn,update_fn): self.tree=[0]*(2*N+2) ...
- [LeetCode] Falling Squares 下落的方块
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...
- 【leetcode】699. Falling Squares
题目如下: On an infinite number line (x-axis), we drop given squares in the order they are given. The i- ...
- 699. Falling Squares
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...
- Java实现 LeetCode 699 掉落的方块(线段树?)
699. 掉落的方块 在无限长的数轴(即 x 轴)上,我们根据给定的顺序放置对应的正方形方块. 第 i 个掉落的方块(positions[i] = (left, side_length))是正方形,其 ...
- [Swift]LeetCode699. 掉落的方块 | Falling Squares
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...
- LeetCode高频题目(100)汇总-Java实现
LeetCode高频题目(100)汇总-Java实现 LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- Falling Squares
2020-01-08 10:16:37 一.Falling squares 问题描述: 问题求解: 本题其实也是一条经典的区间问题,对于区间问题,往往可以使用map来进行区间的维护操作. class ...
- [LeetCode] Bricks Falling When Hit 碰撞时砖头掉落
We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only i ...
随机推荐
- 一图速览 | DTCC 2021大会,阿里云数据库技术大咖都聊了些什么?
简介: 3天9场干货分享,快来收藏吧! 10月18日~10月20日, 由国内知名IT技术社区主办的数据库技术交流盛会--DTCC 2021 (第十一届中国数据库技术大会)在京圆满落幕.大会以&quo ...
- dotnet 7 已知问题 WPF 的 TreeView 开启虚拟化之后只显示首项
本文记录 WPF 在 dotnet 7 的一个已知问题,此问题当前已修复,只需更新 SDK 或运行时即可.使用 TreeView 在开启虚拟化之后只显示首项,其他项不显示.本文将告诉大家此问题的原因和 ...
- dotnet 构建还原失败 NuGet.targets 错误可能原因
我在一次断电关机之后,发现我所有的项目都构建不通过了,提示在 NuGet.targets 文件的第 130 行错误.原因就是存在有某个被项目引用的 NuGet 包被损坏,在进行 NuGet 还原时读取 ...
- 2018-8-10-win10-uwp-如何开始写-uwp-程序
title author date CreateTime categories win10 uwp 如何开始写 uwp 程序 lindexi 2018-08-10 19:16:50 +0800 201 ...
- Ubuntu VNC 远程桌面及常见问题
一.Ubuntu 远程桌面开启 在ubuntu 设置中打开远程桌面 **注意:如果没有共享桌面选项也不要谎,只需要安装 vino 即可 sudo apt update sudo apt install ...
- RT-Thread线程同步与线程通信
一.线程同步 线程同步的使用场景 例如一项工作中的两个线程:一个线程从传感器中接收数据并且将数据写到共享内存中,同时另一个线程周期性的从共享内存中读取数据并发送去显示,下图描述了两个线程间的数据传递: ...
- Python——基本输入和输出
Python提供了基本的输入和输出功能,这些功能通常是通过内置的input()函数(用于输入)和print()函数(用于输出)来实现的.以下是这两个函数的详细描述和示例: 1. print() 函数( ...
- 报错:Client does not support authentication protocol requested by server; consider upgrading MySQL cli
IDEA启动项目登录时显示用户或密码错误 或者 连接mysql数据库时报错 原因: mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是ca ...
- Vben-admin---ApiSelect Invalid prop: type check failed for prop "onUpdate:value". Expected Function, got Array
在basicFrom组件里添加一个ApiSelect, <template #localSearch="{ model, field }"> <ApiSelect ...
- ip地址、子网掩码、网关、dns简介
IP地址IPADDR: IP地址是唯一的标识,是一段网络编码(二进制),由32位组成. IP 是 Internet Protocol(网际互连协议)的缩写,是 TCP/IP 体系中的网络层协议. IP ...