当前位置:网站首页>力扣解法汇总699-掉落的方块
力扣解法汇总699-掉落的方块
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
在无限长的数轴(即 x 轴)上,我们根据给定的顺序放置对应的正方形方块。
第 i 个掉落的方块(positions[i] = (left, side_length))是正方形,其中 left 表示该方块最左边的点位置(positions[i][0]),side_length 表示该方块的边长(positions[i][1])。
每个方块的底部边缘平行于数轴(即 x 轴),并且从一个比目前所有的落地方块更高的高度掉落而下。在上一个方块结束掉落,并保持静止后,才开始掉落新方块。
方块的底边具有非常大的粘性,并将保持固定在它们所接触的任何长度表面上(无论是数轴还是其他方块)。邻接掉落的边不会过早地粘合在一起,因为只有底边才具有粘性。
返回一个堆叠高度列表 ans 。每一个堆叠高度 ans[i] 表示在通过 positions[0], positions[1], ..., positions[i] 表示的方块掉落结束后,目前所有已经落稳的方块堆叠的最高高度。
示例 1:
输入: [[1, 2], [2, 3], [6, 1]]
输出: [2, 5, 5]
解释:
第一个方块 positions[0] = [1, 2] 掉落:
_aa
_aa
-------
方块最大高度为 2 。
第二个方块 positions[1] = [2, 3] 掉落:
__aaa
__aaa
__aaa
_aa__
_aa__
--------------
方块最大高度为5。
大的方块保持在较小的方块的顶部,不论它的重心在哪里,因为方块的底部边缘有非常大的粘性。
第三个方块 positions[1] = [6, 1] 掉落:
__aaa
__aaa
__aaa
_aa
_aa___a
--------------
方块最大高度为5。
因此,我们返回结果[2, 5, 5]。
示例 2:
输入: [[100, 100], [200, 100]]
输出: [100, 100]
解释: 相邻的方块不会过早地卡住,只有它们的底部边缘才能粘在表面上。
注意:
1 <= positions.length <= 1000.
1 <= positions[i][0] <= 10^8.
1 <= positions[i][1] <= 10^6.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/falling-squares
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 记录一个按照left大小排序的集合,集中的元素为Model。每次落下的时候,遍历集合中的元素,更新Model的值。 * 因为是有序排列的,所以如果当前值的right大于遍历的Model的right,则就可以跳出循环了。 * 具体代码没有实现,和官方的解法2比较像,所以直接使用官方解决2的代码了。
代码:
public class Solution699 {
public List<Integer> fallingSquares(int[][] positions) {
int n = positions.length;
List<Integer> ret = new ArrayList<Integer>();
TreeMap<Integer, Integer> heightMap = new TreeMap<Integer, Integer>();
heightMap.put(0, 0); // 初始时从 0 开始的所有点的堆叠高度都是 0
for (int i = 0; i < n; i++) {
int size = positions[i][1];
int left = positions[i][0], right = positions[i][0] + positions[i][1] - 1;
Integer lp = heightMap.higherKey(left), rp = heightMap.higherKey(right);
Integer prevRightKey = rp != null ? heightMap.lowerKey(rp) : heightMap.lastKey();
int rHeight = prevRightKey != null ? heightMap.get(prevRightKey) : 0; // 记录 right + 1 对应的堆叠高度(如果 right + 1 不在 heightMap 中)
// 更新第 i 个掉落的方块的堆叠高度
int height = 0;
Integer prevLeftKey = lp != null ? heightMap.lowerKey(lp) : heightMap.lastKey();
Map<Integer, Integer> tail = prevLeftKey != null ? heightMap.tailMap(prevLeftKey) : heightMap;
for (Map.Entry<Integer, Integer> entry : tail.entrySet()) {
if (entry.getKey() == rp) {
break;
}
height = Math.max(height, entry.getValue() + size);
}
// 清除 heightMap 中位于 (left, right] 内的点
Set<Integer> keySet = new TreeSet<Integer>(tail.keySet());
for (Integer tmp : keySet) {
if (lp == null || tmp < lp) {
continue;
}
if (rp != null && tmp >= rp) {
break;
}
heightMap.remove(tmp);
}
heightMap.put(left, height); // 更新 left 的变化
if (rp == null || rp != right + 1) { // 如果 right + 1 不在 heightMap 中,更新 right + 1 的变化
heightMap.put(right + 1, rHeight);
}
ret.add(i > 0 ? Math.max(ret.get(i - 1), height) : height);
}
return ret;
}
static class Model {
int start = 0;
int end = 0;
int height = 0;
public Model(int start, int end, int length) {
this.start = start;
this.end = end;
this.height = length;
}
}
}边栏推荐
- Software engineering course: Chapter 2 software problem definition and feasibility analysis after class exercises
- ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法
- A mystery of the end of vagrant up
- Graphic data analysis | business cognition and data exploration
- Three main factors determining advertising quality
- 初探性能优化!从2个月到4小时的性能提升!
- Graphic data analysis | data cleaning and pretreatment
- Quatre schémas de mise en file d'attente des messages pour redis
- 聯調這夜,我把同事打了...
- Niuke monthly race 14- simple counting
猜你喜欢

Does the virtual host have independent IP

Subject knowledge and educational ability of information technology in the first half of 2019 – subjective questions

The establishment and introduction of the announcement module of PHP development blog system

混泥土(地面+墙面)+ 山体裂缝数据集汇总(分类及目标检测)

CVPR2022 | iFS-RCNN:一种增量小样本实例分割器

Four schemes for redis to implement message queue

Alicloud OSS file upload system

MySQL表常用操作思维导图

如何为Excel中的单元格自动填充颜色

通用树形结构的迭代与组合模式实现方案
随机推荐
matplotlib. pyplot. Bar chart (II)
php安全开发 12博客系统的 系统模块信息的修改
The establishment and introduction of the announcement module of PHP development blog system
How can low code platforms improve cost effectiveness?
pip运行报错:Fatal error in launcher: Unable to create process using
[learn FPGA programming from scratch -19]: quick start chapter - operation steps 4-1- Verilog software download and construction of development environment - Altera quartz II version
自适应搜索广告有哪些优势?
关于vagrant up的一个终结之谜
力扣解法汇总732-我的日程安排表 III
Design principle [Demeter's Law]
C asynchronous programming from simple to deep (III) details awaiter
Pagination writing of PHP security open 10 article list module
[C language] summary of basic knowledge points of pointer
Design practice of rongyun Im on electron platform
大一下期:学习总结
入手Ticwatch2
Niuke monthly race 14- simple counting
In Net platform using reflectiondynamicobject to optimize reflection calling code
力扣编程题-解法汇总
Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)