【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
ansof 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 ...
随机推荐
- 【已转移】【Java架构:系统架构理论】一篇文章搞掂:RESTful
一.定义 1.起源 来源:Roy Fielding的博士论文. 目的:理解和评估以网络为基础的应用软件的架构设计,得到一个功能强.性能好.适宜通信的架构. 定义:一种实现软件通信的架构风格.设计风格, ...
- win10下VMware15运行ubuntu18.04无法和主机之间复制粘贴问题
可以运行以下命令行: sudo apt-get autoremove open-vm-tools sudo apt-get install open-vm-tools sudo apt-get ins ...
- Malformed UTF-8 characters, possibly incorrectly encoded 或中文乱码 (Uncaught InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in)
问题: Uncaught InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in 是 ...
- C# 做延迟,但系统又能同时能执行其它任务
private void Delay(int Millisecond) //使用时直接调用即可 { DateTime current = DateTime.Now; while (current.Ad ...
- 第 4 章 前端基础之jquery
一.jQuery是什么? 1. jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. 2. jQuery是继prototype之后又一 ...
- 【Unity 系统知识】 Time类
[转载请注明出处] //表示时间总量Time.time:(只读)表示从程序运行的总时间,会随着游戏的暂停而停止计算.Time.unscaledTime:(只读)不考虑timescale对时间修改的总时 ...
- Java8数据流
流/Stream是在JAVA8中引入的一个抽象,可以处理类似SQL语句声明数据. 例如,考虑下面的SQL语句. SELECT max(salary),employee_id,employee_name ...
- vim插件管理器:Vundle的介绍及安装(很全)(转载)
转载自:https://blog.csdn.net/zhangpower1993/article/details/52184581 背景 Vim缺乏默认的插件管理器,所有插件的文件都散布在~/.vim ...
- [51nod 1681]公共祖先(dfs序+线段树合并)
[51nod 1681]公共祖先(dfs序+线段树合并) 题面 给出两棵n(n<=100000)个点的树,对于所有点对求它们在两棵树中公共的公共祖先数量之和. 如图,对于点对(2,4),它们在第 ...
- Day10---Python的jieba库
jieba库:中文分词第三方库 jieba.lcut(s) jieba.lcut(s,cut_all=true) jieba.lcut_for_search(s)