【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 beingpositions[i][0]
and sidelengthpositions[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 heightans[i]
represents the current highest height of any square we have dropped, after dropping squares represented bypositions[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.Note:
1 <= positions.length <= 1000
.1 <= positions[i][0] <= 10^8
.1 <= positions[i][1] <= 10^6
.
解题思路:positions.length最大是1000,因此解题算法的时间复杂度应该允许在O(n^2)。O(n^2)的解法也很简单直接,遍历positions,把positions[i]与positions[0]~positions[i-1]之前的所有元素比较检查是否有相交,如果与positions[j]相交,那么positions[i]的高度就是positions[j]的高度加上positions[i]自身的高度。
代码如下:
class Solution(object):
def fallingSquares(self, positions):
"""
:type positions: List[List[int]]
:rtype: List[int]
"""
history = []
res = []
altitude = []
for i, (pos,length) in enumerate(positions):
altitude.append(length)
for j,(hisPos,hisLength) in enumerate(history):
if (pos + length <= hisPos or hisPos + hisLength <= pos) == False:
altitude[-1] = max(altitude[-1],length+altitude[j])
if len(res) == 0:
res.append(altitude[-1])
else:
res.append(max(res[-1],altitude[-1]))
history.append([pos,length])
return res
【leetcode】699. Falling Squares的更多相关文章
- 【LeetCode】279. Perfect Squares 解题报告(C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四平方和定理 动态规划 日期 题目地址:https: ...
- 【LeetCode】840. Magic Squares In Grid 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 利用河图规律 暴力解法 日期 题目地址:https: ...
- 【LeetCode】并查集 union-find(共16题)
链接:https://leetcode.com/tag/union-find/ [128]Longest Consecutive Sequence (2018年11月22日,开始解决hard题) 给 ...
- 【leetcode】688. Knight Probability in Chessboard
题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- [CSP-S模拟测试]:X国的军队(贪心)
题目描述 $X$国和$Y$国开战了! 作为$X$国的军事参谋,你了解到事态的严峻性.为了更好地应付敌人,你收集到了$Y$国城市中$n$个据点的信息,你打算攻破这$n$个据点! 每个据点$i$的信息由火 ...
- ubuntu14编译安装qt5.0.1
http://hi.baidu.com/houxn22/item/d652f29dec4a701f924f41a0 1.进入官网:http://qt-project.org/downloads下载对应 ...
- Jira中的Tempo查看component以及issue的工作量汇总
在右侧group by的地方,同时选中component和issue
- Oracle命令行模式,批量执行SQL脚本
由于项目不同,使用的数据库也不一样,通常MySQL 比较方便简介,相对而言Oracle比较繁琐一点,尤其是堡垒机的连接的时候, 通过堡垒机登陆,数据库服务器,通过下面的脚本执行进入到命令行模式执行SQ ...
- UVA 111 历史考试
题目描述:最长公共子序列的变形 题目序列中第i项是学生给第i号历史事件排出的序号,另外还给出了第i号历史事件的正确序号 求按照学生给出的序号排好历史事件后,所得的事件排序与历史事件实际发生的序列的最长 ...
- PADS 学习资料
PADS软件: PADS9.5_3in1.7z 链接: http://pan.baidu.com/s/1epO4Y 密码: zltl 打不开程序的博友,请看你的电脑有没有安装常用软件运行库 ...
- Oracle-创建索引分区
对大数据量索引进行分区同样能够优化应用系统的性能.一般来说,如果索引所对应的表的数据量非常大,比如几百万甚至上千万条数据,则索引也会占用很大的空间,这时,建议对索引进行分区. Oracle索引分区分为 ...
- 拒绝从入门到放弃_《鸟哥的 Linux 私房菜 — 基础学习篇(第三版)》必读目录
目录 目录 前言 关于这本书 必看知识点 最后 前言 相信部分刚进入这个行业的新同学会对一个问题感到疑惑,为什么从培训学校出来的学员不被欢迎? 这里记录下一些我个人的看法(博主也曾有面试新员工的经历) ...
- C语言的未初始化的数组的值为什么是随机的
突然想起来前几天同学问我为什么没有初始化的数组的值是随机的,发现这个困惑自己也是存在的,所以自己总结的心得. 1. 首先,并不是所有未初始化的数组的值都是随机的.对于没有初始化的数组,分两种情况: ( ...
- Web控件LinkButton
<asp:LinkButton ID="" runat="server" ></asp:LinkButton> 编译后就变成了回发事件 ...