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.

分析

首先还是来明确一下题目的意思,在x轴上落下方块,用方块的最左下的顶点positions[i][0]和方块的长度positions[i][1]来确定每个方块的位置,那么方块的高是什么呢?查了一下,原来squre是正方形的意思,那么每个方块的高度就知道了。将这些箱子从无限的高空中投掷到x轴,边有交集的箱子会堆叠(边界相接不算交集),查询每个箱子投掷后的最大堆叠高度。

是不是有点像俄罗斯方块?解法是首先利用之前算法题遇到的interval来代表这些方块,初始假设所有的方块都落到了地上而不会堆叠,对于每个方块我们去迭代它之前所有的方块,检查是否有方块是应该在当前方块的下面的,如果当前的interva和之前的interval有交集那么则意味着当前的方块应该在这个方块之上。我们的目标是找到那个最高的square并且将当前方块cur放在之前的方块i之上,并且将方块cur的高度设置为

cur.height = cur.height + previousMaxHeight;

要明确的是这里的cur.height始终记录的是放下当前cur方块后整个x轴上堆叠的最大高度。previousMaxHeight记录的是在的当前方块cur之下的堆叠到的最大高度。

还是有些不是很明白,还是上代码来具体分析

class Solution {
private class Interval {
int start, end, height;
public Interval(int start, int end, int height) {
this.start = start;
this.end = end;
this.height = height;
}
}
public List<Integer> fallingSquares(int[][] positions) {
List<Interval> intervals = new ArrayList<>();
List<Integer> res = new ArrayList<>();
int h = 0;
for (int[] pos : positions) {
Interval cur = new Interval(pos[0], pos[0] + pos[1] - 1, pos[1]);
h = Math.max(h, getHeight(intervals, cur));
res.add(h);
}
return res;
}
private int getHeight(List<Interval> intervals, Interval cur) {
int preMaxHeight = 0;  // 注意这里preMaxHeight会初始化为0,这样能保证后面的preMaxHeight一定是beneath cur的
for (Interval i : intervals) {  // 从intervas取出的interval都是堆叠高度合并之后的
// Interval i does not intersect with cur
if (i.end < cur.start) continue;
if (i.start > cur.end) continue;
// find the max height beneath cur
preMaxHeight = Math.max(preMaxHeight, i.height);
}
cur.height += preMaxHeight;  // 确定cur的高度,并将这个cur加入到intervas中,注意这个高度是合并之后再加入到interva中的
intervals.add(cur);
return cur.height;
}
}

LeetCode699. Falling Squares的更多相关文章

  1. Falling Squares

    2020-01-08 10:16:37 一.Falling squares 问题描述: 问题求解: 本题其实也是一条经典的区间问题,对于区间问题,往往可以使用map来进行区间的维护操作. class ...

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

  3. [LeetCode] Falling Squares 下落的方块

    On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...

  4. 699. Falling Squares

    On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...

  5. 【leetcode】699. Falling Squares

    题目如下: On an infinite number line (x-axis), we drop given squares in the order they are given. The i- ...

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

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

  7. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. vue学习记录

    vue中常用的指令 v-model 双向数据绑定,一般用于表单元素 v-for 对数组或对象进行循环操作,使用的是v-for <!-- 普通循环 --><li v-for=" ...

  2. C/C++中的回调函数

    在理解“回调函数”之前,首先讨论下函数指针的概念. 函数指针 (1)概念:指针是一个变量,是用来指向内存地址的.一个程序运行时,所有和运行相关的物件都是需要加载到内存中,这就决定了程序运行时的任何物件 ...

  3. ubuntu 使用小技巧

    1. 查看网速 ethstatus ubuntu下用ethstatus可以监控实时的网卡带宽占用.这个软件能显示当前网卡的 RX 和 TX 速率,单位是Byte 安装 ethstatus 软件 sud ...

  4. MongoDB 之 aggregate $group 巧妙运用

    有这样一组数据: { "campaign_id": "A", "campaign_name": "A", "s ...

  5. http协议POST请求头content-type主要的四种取值

    介绍: 在此之前对content-type理解很肤浅,因此必须记录下来现在的理解,以便回顾 Content-Type,从名字上可以理解为内容类型,但在互联网上专业术语叫“媒体类型”,即MediaTyp ...

  6. TPL概要

    ReaderWriterLockSlim.读写锁,允许多个线程同时获取读锁,但同一时间只允许一个线程获得写锁,因此也称作共享-独占锁.只有等到对象被写入锁占用的时候,才会阻塞 Barrier .它允许 ...

  7. AspNetPager控件的最基本用法

    AspNetPager控件是一个基于.net的第三方免费开源控件,具有开发高效.使用方便.功能完整等优点.它弥补了GridView内置分页以及PageDatasource类辅助分页的不足,将分页数据逻 ...

  8. win7下设置挂载Linux服务器nfs共享的数据 -- 转

    最近学习NFS文件系统的使用,Ubuntu上配置好了,想和Win7共享数据,所以网上搜到了这篇文章.借花献佛,跟大家共享一下: http://www.2cto.com/os/201207/139132 ...

  9. Python概念-Item系列(林海峰教的)

    这个Item系列是egon老师自创的,个人还是可以接受这种文化底蕴的,所以直接拿来用,也是毫无违和感的 所谓Attr系列,其实是__setattr__,__delattr__,__getattr__ ...

  10. Python概念-反射之文化底蕴版:反正射了

    什么是反射 光在两种物质分界面上改变传播方向又返回原来物质中的现象,叫反射! 以上,是反射的物理定义,与python中的反射概念,完全没有任何关系 书归正传:反射 就是通过字符串的形式,操作对象相关的 ...