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 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.
Note:
1 <= positions.length <= 1000.1 <= positions[i][0] <= 10^8.1 <= positions[i][1] <= 10^6.
Approach #1: C++. [Brute Force]
class Solution {
public:
vector<int> fallingSquares(vector<pair<int, int>>& positions) {
vector<int> ans;
vector<Interval> intervals;
int maxHeight = INT_MIN;
for (const auto& it : positions) {
int start = it.first;
int end = start + it.second;
int baseHeight = 0;
for (const auto& it : intervals) {
if (start >= it.end || end <= it.start) {
continue;
}
baseHeight = max(baseHeight, it.height);
}
int height = it.second + baseHeight;
maxHeight = max(maxHeight, height);
intervals.push_back(Interval(start, end, height));
ans.push_back(maxHeight);
}
return ans;
}
private:
struct Interval {
int start;
int end;
int height;
Interval(int start, int end, int height)
: start(start), end(end), height(height) {}
};
};
Approach #2: C++. [Using Map]
class Solution {
public:
vector<int> fallingSquares(vector<pair<int, int>>& positions) {
vector<int> ans;
map<pair<int, int>, int> b;
int maxHeight = INT_MIN;
for (const auto& kv : positions) {
int start = kv.first;
int size = kv.second;
int end = start + size;
auto it = b.upper_bound({start, end});
if (it != b.begin()) {
auto it2 = it;
if ((--it2)->first.second > start)
it = it2;
}
int baseHeight = 0;
vector<tuple<int, int, int>> ranges;
while (it != b.end() && it->first.first < end) {
const int s = it->first.first;
const int e = it->first.second;
const int h = it->second;
if (s < start) ranges.emplace_back(s, start, h);
if (e > end) ranges.emplace_back(end, e, h);
baseHeight = max(baseHeight, h);
it = b.erase(it);
}
int newHeight = size + baseHeight;
b[{start, end}] = newHeight;
for (const auto& range : ranges) {
b[{get<0>(range), get<1>(range)}] = get<2>(range);
}
maxHeight = max(maxHeight, newHeight);
ans.push_back(maxHeight);
}
return ans;
}
};
Notes:
Approach #3: Java. [segment tree]
class Solution {
public List<Integer> fallingSquares(int[][] positions) {
int n = positions.length;
Map<Integer, Integer> cc = coorCompression(positions);
int best = 0;
List<Integer> res = new ArrayList<>();
SegmentTree tree = new SegmentTree(cc.size());
for (int[] pos : positions) {
int L = cc.get(pos[0]);
int R = cc.get(pos[0] + pos[1] - 1);
int h = tree.query(L, R) + pos[1];
tree.update(L, R, h);
best = Math.max(best, h);
res.add(best);
}
return res;
}
private Map<Integer, Integer> coorCompression(int[][] positions) {
Set<Integer> set = new HashSet<>();
for (int[] pos : positions) {
set.add(pos[0]);
set.add(pos[0] + pos[1] - 1);
}
List<Integer> list = new ArrayList<>(set);
Collections.sort(list);
Map<Integer, Integer> map = new HashMap<>();
int t = 0;
for (int pos : list) map.put(pos, t++);
return map;
}
class SegmentTree {
int[] tree;
int N;
SegmentTree(int N) {
this.N = N;
int n = (1 << ((int) Math.ceil(Math.log(N) / Math.log(2)) + 1));
tree = new int[n];
}
public int query(int L, int R) {
return queryUtil(1, 0, N - 1, L, R);
}
private int queryUtil(int index, int s, int e, int L, int R) {
// out of range
if (s > e || s > R || e < L) {
return 0;
}
// [L, R] cover [s, e]
if (s >= L && e <= R) {
return tree[index];
}
// Overlapped
int mid = s + (e - s) / 2;
return Math.max(queryUtil(2 * index, s, mid, L, R), queryUtil(2 * index + 1, mid + 1, e, L, R));
}
public void update(int L, int R, int h) {
updateUtil(1, 0, N - 1, L, R, h);
}
private void updateUtil(int index, int s, int e, int L, int R, int h) {
// out of range
if (s > e || s > R || e < L) {
return;
}
tree[index] = Math.max(tree[index], h);
if (s != e) {
int mid = s + (e - s) / 2;
updateUtil(2 * index, s, mid, L, R, h);
updateUtil(2 * index + 1, mid + 1, e, L, R, h);
}
}
}
}
699. Falling Squares的更多相关文章
- 【leetcode】699. Falling Squares
题目如下: On an infinite number line (x-axis), we drop given squares in the order they are given. The i- ...
- leetcode 699. Falling Squares 线段树的实现
线段树实现.很多细节值得品味 都在注释里面了 class SegTree: def __init__(self,N,query_fn,update_fn): self.tree=[0]*(2*N+2) ...
- Falling Squares
2020-01-08 10:16:37 一.Falling squares 问题描述: 问题求解: 本题其实也是一条经典的区间问题,对于区间问题,往往可以使用map来进行区间的维护操作. class ...
- [LeetCode] Falling Squares 下落的方块
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...
- [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 ...
- LeetCode699. Falling Squares
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- 创建spring管理的自定义注解
转自: http://blog.csdn.net/wuqiqing_1/article/details/52763372 Annotation其实是一种接口.通过Java的反射机制相关的API来访问A ...
- gradle 跳过junitTest的方法
Web项目中不长会写JunitTest,但也会写.gradle build的时候回执行test 这项task.如果想跳过,通常有几种方法: 1.在build.gradle 文件中禁用task test ...
- mysql错误:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated
今天迁移django数据库的时候,跑程序的时候出现这样的错误: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY cla ...
- Quartz的misfire理解
misfire用于Trigger触发时,线程池中没有可用的线程或者调度器关闭了,此时这个Trigger变为misfire.当下次调度器启动或者有可以线程时,会检查处于misfire状态的Trigger ...
- 关于NIO编程
NIO概述 什么是NIO? Java NIO(New IO)是一个可以替代标准Java IO API的IO API(从Java 1.4开始),Java NIO提供了与标准IO不同的IO工作方式. Ja ...
- 对于glut和freeglut的一点比较和在VS2013上的配置问题
先大概说一下glut.h和freeglut.h 首先要知道openGL是只提供绘图,不管窗口的,所以你需要给它一个绘图的区域(openGL能跨平台也与此有些关系) glut.h和freeglut.h都 ...
- Spark- Linux下安装Spark
Spark- Linux下安装Spark 前期部署 1.JDK安装,配置PATH 可以参考之前配置hadoop等配置 2.下载spark-1.6.1-bin-hadoop2.6.tgz,并上传到服务器 ...
- 草原psd素材
草原PSD素材,草原,风景,蓝天白云,飞鸟,阳光,绿色,草地. http://www.huiyi8.com/caoyuan/psd/
- 粉红色织梦CMS企业模板
粉红色织梦CMS企业网站模板,粉红色,织梦CMS,织梦企业模板,CMS模板. 模板地址:http://www.huiyi8.com/sc/7247.html
- HDU 4652 Dice:期望dp(成环)【错位相减】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4652 题意: 给你一个有m个面的骰子. 两种询问: (1)"0 m n": “最后 ...