Java实现 LeetCode 699 掉落的方块(线段树?)
699. 掉落的方块
在无限长的数轴(即 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.
PS:
按照左端点放进树
import java.util.*;
class Solution {
// 描述方块以及高度
private class Node {
int l, r, h, maxR;
Node left, right;
public Node(int l, int r, int h, int maxR) {
this.l = l;
this.r = r;
this.h = h;
this.maxR = maxR;
this.left = null;
this.right = null;
}
}
//
public List<Integer> fallingSquares(int[][] positions) {
// 创建返回值
List<Integer> res = new ArrayList<>();
// 根节点,默认为零
Node root = null;
// 目前最高的高度
int maxH = 0;
for (int[] position : positions) {
int l = position[0]; // 左横坐标
int r = position[0] + position[1]; // 右横坐标
int e = position[1]; // 边长
int curH = query(root, l, r); // 目前区间的最高的高度
root = insert(root, l, r, curH + e);
maxH = Math.max(maxH, curH + e);
res.add(maxH);
}
return res;
}
private Node insert(Node root, int l, int r, int h) {
if (root == null) return new Node(l, r, h, r);
if (l <= root.l)
root.left = insert(root.left, l, r, h);
else
root.right = insert(root.right, l, r, h);
// 最终目标是仅仅需要根节点更新 maxR
root.maxR = Math.max(r, root.maxR);
return root; // 返回根节点
}
private int query(Node root, int l, int r) {
// 新节点的左边界大于等于目前的maxR的话,直接得到0,不需要遍历了
if (root == null || l >= root.maxR) return 0;
// 高度
int curH = 0;
if (!(r <= root.l || root.r <= l)) // 是否跟这个节点相交
curH = root.h;
// 剪枝
curH = Math.max(curH, query(root.left, l, r));
if (r > root.l)
curH = Math.max(curH, query(root.right, l, r));
return curH;
}
}
Java实现 LeetCode 699 掉落的方块(线段树?)的更多相关文章
- Java实现 LeetCode 814 二叉树剪枝 (遍历树)
814. 二叉树剪枝 给定二叉树根结点 root ,此外树的每个结点的值要么是 0,要么是 1. 返回移除了所有不包含 1 的子树的原二叉树. ( 节点 X 的子树为 X 本身,以及所有 X 的后代. ...
- Java实现 LeetCode 648 单词替换(字典树)
648. 单词替换 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词--我们称这个词为 继承词(successor).例如,词根an,跟随着单词 other( ...
- Java实现 LeetCode 617 合并二叉树(遍历树)
617. 合并二叉树 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点 ...
- C#LeetCode刷题-线段树
线段树篇 # 题名 刷题 通过率 难度 218 天际线问题 32.7% 困难 307 区域和检索 - 数组可修改 42.3% 中等 315 计算右侧小于当前元素的个数 31.9% 困难 4 ...
- leetcode 699. Falling Squares 线段树的实现
线段树实现.很多细节值得品味 都在注释里面了 class SegTree: def __init__(self,N,query_fn,update_fn): self.tree=[0]*(2*N+2) ...
- 【LeetCode】线段树 segment-tree(共9题)+ 树状数组 binary-indexed-tree(共5题)
第一部分---线段树:https://leetcode.com/tag/segment-tree/ [218]The Skyline Problem [307]Range Sum Query - Mu ...
- [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 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- 【Hadoop离线基础总结】Hue的简单介绍和安装部署
目录 Hue的简单介绍 概述 核心功能 安装部署 下载Hue的压缩包并上传到linux解压 编译安装启动 启动Hue进程 hue与其他框架的集成 Hue与Hadoop集成 Hue与Hive集成 Hue ...
- Linux设备子系统初始化
本文介绍的内容是基于Linux3.1源码,并参考了很多网上找来的资料 Linux内核的启动的流程如下: start_kernel->rest_init->kernel_init->d ...
- Properties集合与IO流
package com.itheima.demo07.Prop; import java.io.FileOutputStream; import java.io.FileReader; import ...
- extract变量覆盖
0x01 extract变量覆盖 <?php $flag='xxx'; extract($_GET); if(isset($shiyan)) { $content=trim(file_get_c ...
- 爬取淘宝商品信息,放到html页面展示
爬取淘宝商品信息 import pymysql import requests import re def getHTMLText(url): kv = {'cookie':'thw=cn; hng= ...
- Docker容器映射到宿主机只有tcp6没有tcp问题
问题描述: Docker容器映射到宿主机后,查询端口连接只有tcp6没有tcp,通过ipv4地址连接时无法连接成功. 处理方法: 1.检查是否开启ipv4端口转发 sysctl net.ipv4.ip ...
- IDEA快捷键(windows)
Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Shift+Click,可以关闭文件Ctrl+[ ...
- 解决 Loaded plugins: fastestmirror
其大概意思是fastestmirror不能使用,fastestmirror是yum的一个加速插件,具体我也没有仔细了解过,可能是系统不支持或者缺少组建导致的.处理办法就是禁用这个插件,方法如下:roo ...
- 在ORACLE中实现SELECT TOP N的方法----[转]
1.在ORACLE中实现SELECT TOP N 由于ORACLE不支持SELECT TOP语句,所以在ORACLE中经常是用ORDER BY跟ROWNUM的组合来实现SELECT TOP N的查询. ...
- Apache 慢连接dos
http://neue.v2ex.com/t/108717------不实用 http://www.blogjava.net/bukebushuo/articles/293776.html http: ...