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 掉落的方块(线段树?)的更多相关文章

  1. Java实现 LeetCode 814 二叉树剪枝 (遍历树)

    814. 二叉树剪枝 给定二叉树根结点 root ,此外树的每个结点的值要么是 0,要么是 1. 返回移除了所有不包含 1 的子树的原二叉树. ( 节点 X 的子树为 X 本身,以及所有 X 的后代. ...

  2. Java实现 LeetCode 648 单词替换(字典树)

    648. 单词替换 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词--我们称这个词为 继承词(successor).例如,词根an,跟随着单词 other( ...

  3. Java实现 LeetCode 617 合并二叉树(遍历树)

    617. 合并二叉树 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点 ...

  4. C#LeetCode刷题-线段树

    线段树篇 # 题名 刷题 通过率 难度 218 天际线问题   32.7% 困难 307 区域和检索 - 数组可修改   42.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 4 ...

  5. leetcode 699. Falling Squares 线段树的实现

    线段树实现.很多细节值得品味 都在注释里面了 class SegTree: def __init__(self,N,query_fn,update_fn): self.tree=[0]*(2*N+2) ...

  6. 【LeetCode】线段树 segment-tree(共9题)+ 树状数组 binary-indexed-tree(共5题)

    第一部分---线段树:https://leetcode.com/tag/segment-tree/ [218]The Skyline Problem [307]Range Sum Query - Mu ...

  7. [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 ...

  8. 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 ...

  9. 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. ...

随机推荐

  1. Programmatically add an application to Windows Firewall

    Programmatically add an application to Windows Firewall 回答1   Not sure if this is the best way, but ...

  2. JVM 虚拟机&&类加载(一)

    虚拟机 虚拟机简介 Java 虚拟机(JVM)是运行java程序的抽象计算机,它是计算机设备的规范,可以采用不同方式进行实现,java 程序通过运行在JVM中实现跨平台,一次编译到处运行,不同的操作系 ...

  3. 一阶RC低通滤波器详解(仿真+matlab+C语言实现)

    文章目录 1 预备知识 2 simulink 仿真 3 simulink 运行结果 4 matlab实现 5 matlab运行结果 6 C语言实现 7 C语言运行结果 如果本文帮到了你,帮忙点个赞: ...

  4. Linux内核驱动学习(九)GPIO外部输入的处理

    文章目录 前言 设备树 两个结构体 gpio_platform_data gpio_demo_device 两种方式 轮询 外部中断 总结 附录 前言 前面是如何操作GPIO进行输出,这里我重新实现了 ...

  5. Ubuntu 配置/etc/fstab参数实现开机自动挂载硬盘

    文章目录 前言 fstab 参数含义 实现步骤 1 查看硬盘信息,并找到需要进行挂载的硬盘 2 sudo mkfs.ext4 /dev/sdc 3 sudo mkdir /home/diska 4 查 ...

  6. 实现简单网页rtmp直播:nginx+ckplayer+linux

    一.安装nginx 安装带有rtmp模块的nginx服务器(其它支持rtmp协议的流媒体服务器像easydarwin.srs等+Apache等web服务器也可以),此处使用nginx服务器,简单方便. ...

  7. [hdu4552]最长公共前缀

    题意:给一个串s,求s的每个前缀出现次数之和. 思路:对于一个后缀i,设i和原串的最长公共前缀为k,则当前总共可以产生k个答案.因此原题转化为求所有后缀与原串的最长公共前缀之和.模板题.以下为通过模板 ...

  8. vue 兄弟组件之间的传值

    一. 子传父,父传子. 二. 1.兄弟之间传递数据需要借助于事件车,通过事件车的方式传递数据 2.创建一个Vue的实例,让各个兄弟共用同一个事件机制. 3.传递数据方,通过一个事件触发bus.$emi ...

  9. JS的函数和对象一

    1.递归 在函数的内部调用自身,默认是一个无限循环. 2.匿名函数 没有名称的函数  function(){   } (1)创建函数 函数声明 function fn1(){   } 函数表达式 va ...

  10. HTML5面试题

    1. sessionStorage和localStorage的区别 答案: sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有在同一个会话中的页面才能访问并且当会 ...