LeetCode699. Falling Squares

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;
    }
}

 

转载于:https://www.cnblogs.com/f91og/p/9742274.html

医学成像学习笔记(一):核磁共振成像(MRI)k空间为何是图像频谱详解 前言   k空间是核磁共振成像图像重建的核心,可能很多人像笔者一样第一次学时会非常疑惑,为何k空间是图像的频域空间,观其填充过程,明明是空域信号的采样填充呀。网上很少有文章讨论这个问题,所以笔者在此写下自己的理解,供大家参考。 MRI位置编码   现在的MRI一次扫描的是一个断层或者多个断层,所谓断层就是一个有厚度的面,通过z方向梯度场来选择。我们扫描一个断层的目的是为了获得该断层各位置的质子情况,它们携带着组织的性质信息,反应出来的就是信号强度。如果整个断层都采用相同的磁场强度,而我们只能得到一个断 阅读详情

相关推荐

使用 syncstart 实现同场景下多个音视频音轨自动化同步

如果需要在视频编辑中同步多个机位的音轨,使得相同的视频能够同时播放,手动操作可能会变得相当复杂。特别是当处理多个视频文件时,例如有10个视频需要互相匹配,实现一对多的同步关系,这就涉及到为每对视频生成匹配的音轨数据。手动完成这样的任务,如生成90套对应的音轨数据,不仅耗时而且容易出错。因此,使用脚本自动化这一过程是一个更为高效和准确的解决方案。通过编写脚本,您可以批量处理多个视频文件,自动同步它们的音轨,大大简化了音视频编辑的工作流程,并确保了同步的准确性。

Mr数据杨 1038

Leetcode699. Falling Squares 699. 掉落的方块

解法 要说难其实也不难……O(n2)O(n^2)O(n2)的方法都可以过 解法一:暴力 每放下一个方块,记录一下放下后这个方块顶端高度,它由两部分组成:一是方块自己的高度,二是以前放下的该方块相交的方块的顶端高度的最大值,由这两部分相加构成 得到每个方块放下时的顶端高度之后,最后的ans数组就是这个高度数组的前缀最大值数组 class Solution(object): def f...

lemonmillie的博客 669

new.zip_SPI FLASH verilog_SPI、verilog_Verilog spi flash_Verilog

使用verilog实现的spi读写flash

C#LeetCode刷题-线段树

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

比特飞 1万+

[Swift]LeetCode699. 掉落的方块 | Falling Squares

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30376323的博客 271

leetcode 699 Falling Squares

用一个二维数组记录当前的总方块情况,每个一维数组有三个元素,分别是区间左端点、右端点,高度。每次加一个木块的时候就对all进行一次遍历,求得当前的最大值。 #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;map&gt; #include &lt;set&gt; #include &lt;queue&gt; using...

DUT_LYH 3万+

C++部署机器学习模型:ONNX转换运行时性能优化实战指南

机器学习模型部署是将训练好的算法模型集成到生产环境的关键步骤,其核心目标是实现稳定、高效且低延迟的推理服务。部署流程通常遵循从模型导出、格式转换到运行时集成的技术链路。ONNX(Open Neural Network Exchange)作为跨框架的模型交换标准,在此过程中扮演了“中间语言”的角色,旨在解决不同训练框架推理引擎间的兼容性问题。然而,在实际工程实践中,ONNX模型转换常面临算子支持不全、精度损失等挑战,而C++运行时集成则需应对内存管理、会话复用及硬件加速等多重性能陷阱。这些技术细节直接关系到

weixin_30580341的博客 548

LeetCode //C - 699. Falling Squares

【代码】LeetCode //C - 699. Falling Squares

Made in Code 1928

leetcode699. Falling Squares

题目如下: On an infinite number line (x-axis), we drop given squares in the order they are given. Thei-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point bein...

weixin_33798152的博客 93

Leetcode699. Falling Squares

题目地址: https://leetcode.com/problems/falling-squares/ 给定一个数学里的二维平面直角坐标系xOyxOyxOy,想象有若干正方形方块掉落,题目保证正方形方块的左下端点的坐标是正整数,也保证方块边长是正整数。想象xxx轴是地面,如果某个方块掉落的过程中遇到了之前的某个方块(擦边而过不算),则该方块会叠到上面。现在给定一个长nnn数组AAA,A[i]A[i]A[i]存了第iii个掉落的方块的信息,其中A[i][0]A[i][0]A[i][0]表示它的左下角的xxx

数学、算法爱好者的博客 281

LeetCode每日一题(699. Falling Squares)

用 coordinate compression 和 segment tree 来解。

wangjun861205的博客 783

leetcode 699. Falling Squares

On an infinite number line (x-axis), we drop given squares in the order they are given. Thei-th square dropped (positions[i] = (left, side_length)) is a square with the left-most point beingpositio...

cmy203的博客 152

leetcode699. 掉落的方块

题目: https://leetcode-cn.com/problems/falling-squares/submissions/ 我的代码不快,但也不至于超时,但是容易理解 每个新的格子进来,之前的格子进行比较,找到最大值 public List<Integer> fallingSquares(int[][] positions) { List<Integer>...

孤竹彧的根据地 500

[Leetcode] 699. Falling Squares 解题报告

题目: 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

魔豆(Magicbean)的博客 686

Java实现 LeetCode 699 掉落的方块(线段树?)

699. 掉落的方块 在无限长的数轴(即 x 轴)上,我们根据给定的顺序放置对应的正方形方块。 第 i 个掉落的方块(positions[i] = (left, side_length))是正方形,其中 left 表示该方块最左边的点位置(positions[i][0]),side_length 表示该方块的边长(positions[i][1])。 每个方块的底部边缘平行于数轴(即 x 轴),并且...

南 墙 1万+

下降路径最小和

本文详细讲解 LeetCode「下降路径最小和」这道经典动态规划题。文章从状态表示、状态转移方程推导、dp 表初始化、填表顺序到返回值,逐步拆解完整解题思路。

知识备忘录 574

LeetCode 350. 两个数组的交集 II|Python 解法详解

这道题的关键是:统计较短数组的元素频次,再扫描另一个数组。理解这一点后,再结合边界条件检查,代码就能保持清晰且稳定。

a1250467048的博客 169

湖面逃跑 · Lake Escape(bfs)

链接:九章算法 - 帮助更多程序员找到好工作,硅谷顶尖IT企业工程师实时在线授课为你传授面试技巧题解:

INGNIGHT的专栏 154

数据结构:二叉树的实现

<think>我们要求根据给定内容生成≤150字的文章摘要。内容是关于二叉树的介绍,包括定义、术语、性质、存储、创建、遍历、统计等。摘要需要概括核心要点。需控制字数在150字以内。注意是“文章摘要”,应简洁概括主要内容。输出直接是摘要文本,不需要额外说明。我们来组织:二叉树是每个节点最多两个子节点的树,有左右之分。常用链式存储,节点含数据及左右指针。创建可用先序+空标记递归。遍历有前序、中序、后序(递归)和层序(队列)。关键操作:求叶子数、节点数、高度、每层节点数,销毁需后序。要点:递归出口、根

Edbse的博客 359

LeetCode双题:滑动窗口破解最小子数组

解法⼀:暴力求解(会超时)算法思路:「从前往后」枚举数组中的任意⼀个元素,把它当成起始位置。然后从这个「起始位置」开始,然后寻找⼀段最短的区间,使得这段区间的和「⼤于等于」⽬标值。将所有元素作为起始位置所得的结果中,找到「最⼩值」即可。

2502_93625125的博客 217
上一篇: LeetCode691. Stickers to Spell Word
dbvz4901
博客等级 码龄11年 6粉丝 0原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值