Falling Squares
2020-01-08 10:16:37
一、Falling squares
问题描述:

问题求解:
本题其实也是一条经典的区间问题,对于区间问题,往往可以使用map来进行区间的维护操作。
class Interval {
int start;
int end;
int 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<Integer> res = new ArrayList<>();
List<Interval> record = new ArrayList<>();
int max_height = Integer.MIN_VALUE;
for (int[] p : positions) {
int s = p[0];
int e = p[0] + p[1];
int h = p[1];
int curr_max = 0;
for (Interval inte : record) {
if (inte.start >= e || inte.end <= s) continue;
curr_max = Math.max(curr_max, inte.height);
}
h += curr_max;
record.add(new Interval(s, e, h));
max_height = Math.max(max_height, h);
res.add(max_height);
}
return res;
}
二、Range module
问题描述:

问题求解:
Range module和上题都可以采用map来进行区间维护得到最终的解,时间复杂度也同样是O(n ^ 2)。
class RangeModule {
class Interval {
int start;
int end;
boolean is_tracked;
public Interval(int start, int end, boolean is_tracked) {
this.start = start;
this.end = end;
this.is_tracked = is_tracked;
}
}
List<Interval> record;
public RangeModule() {
record = new ArrayList<>();
}
public void addRange(int s, int e) {
List<Interval> del = new ArrayList<>();
List<Interval> add = new ArrayList<>();
for (Interval inte : record) {
if (inte.start >= e || inte.end <= s) continue;
del.add(inte);
if (s <= inte.start && e >= inte.end) continue;
else if (s <= inte.start) add.add(new Interval(e, inte.end, true));
else if (e >= inte.end) add.add(new Interval(inte.start, s, true));
else {
add.add(new Interval(inte.start, s, true));
add.add(new Interval(e, inte.end, true));
}
}
for (Interval inte : del) record.remove(inte);
for (Interval inte : add) record.add(inte);
record.add(new Interval(s, e, true));
}
public boolean queryRange(int s, int e) {
int target = e - s;
int curr = 0;
for (Interval inte : record) {
if (inte.start >= e || inte.end <= s) continue;
int l = Math.max(inte.start, s);
int r = Math.min(inte.end, e);
curr += r - l;
}
return curr == target;
}
public void removeRange(int s, int e) {
List<Interval> del = new ArrayList<>();
List<Interval> add = new ArrayList<>();
for (Interval inte : record) {
if (inte.start >= e || inte.end <= s) continue;
del.add(inte);
if (s <= inte.start && e >= inte.end) continue;
else if (s <= inte.start) add.add(new Interval(e, inte.end, true));
else if (e >= inte.end) add.add(new Interval(inte.start, s, true));
else {
add.add(new Interval(inte.start, s, true));
add.add(new Interval(e, inte.end, true));
}
}
for (Interval inte : del) record.remove(inte);
for (Interval inte : add) record.add(inte);
}
}
Falling Squares的更多相关文章
- [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 ...
- 699. Falling Squares
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...
- 【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) ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 解密JDK8 枚举
写一个枚举类 1 2 3 4 5 6 public enum Season { SPRING, SUMMER, AUTUMN, WINTER } 然后我们使用javac编译上面的类,得到class文件 ...
- 腾讯入股Snap,能救“阅后即焚”的命吗?
互联网社交的强大包容性,让各种社交形式都能有着较多的受众群体.普适性极广的QQ.微信."脸谱":专攻陌生人社交的陌陌:让人们发布意见的微博--当然也少不了"阅 ...
- 数据大爆炸:KDD 2016
2016"> 饕餮盛宴 ACM SIGKDD国际会议(简称KDD)是数据挖掘领域的顶级国际会议,由ACM (计算机协会)的数据挖掘及知识发现专委会(SIGKDD)负责组织筹办.在现今如 ...
- 先治再扶,重灾区后的P2P你还敢投吗?
互联网强大的包容性和创新性,给予很多新生事物成长的空间.而其全面普及与快速传播的特性,也让任何事物都像被放在放大镜乃至显微镜下,几乎无形遁形.这样一来,新生事物很容易被"神化" ...
- python列表解析补充:
python列表解析补充: # 补充: f = [x + y for x in 'ABCDE' for y in '1234567'] print(f) test = [] for x in 'ABC ...
- 一次js自定义播放器,canvas绘制弹幕的尝试
不多bb,就直接说实现了什么功能: 1. 视频播放进度调整 2. 视频小窗口实时预览 3. 声音调整 4. 画中画模式 5. 网页全屏 6. 视频全屏 7. canvas绘制弹幕 8. 选中弹幕悬停 ...
- 7-3 jmu-python-回文数判断(5位数字) (10 分)
本题目要求输入一个5位自然数n,如果n的各位数字反向排列所得的自然数与n相等,则输出‘yes’,否则输出‘no’. 输入格式: 13531 输出格式: yes 输入样例1: 13531 输出样例1: ...
- es6中的属性名表达式
代码如下: 问题: 为什么我可以这样给obj1对象添加动态属性? 为什么我最终的结果是只添加了right属性? 解答: 1. 第一个问题解答如下: 我们知道在es5中给对象添加属性有两种方法,一种是通 ...
- HTML中的meta标签常用属性及其作用总结
文章同步到github 以前没怎么太注意过meta标签的作用,只是简单了解一些常用属性,现在结合个人了解的进行记录与总结: 元数据 首先需要了解一下元数据(metadata)元素的概念,用来构建HTM ...
- chorme浏览器记住密码后input黄色背景处理方法总结(三种)
问题分析 正常情况: 记住密码后访问: 解决方法 方法1:阴影覆盖input:-webkit-autofill { -webkit-box-shadow: 0 0 0 1000px white ins ...