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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 15.uboot study 串口初始化
3. 串口初始化 4. 代码实现 关于串口 对于嵌入式设备的开发,刚开始好多设备都无法使用,由于无法获得程序的运行状态,调试程序需要花费好多时间和精力,因此串口对于嵌入式程序的调试的作用显而易见,当串 ...
- 5G-NR物理信道与调制-下行链路v1.1.0
上接<5G-NR物理信道与调制v1.1.0>下行链路 References Definitions, symbols and abbreviations 帧结构与物理资源 通用函数 上行链 ...
- 关于vue+element-ui项目的分页,返回默认显示第一页的问题解决
关于vue+element-ui项目的分页,返回默认显示第一页的问题解决 问题描述 当前页面如下: 然后点击页码跳到第3页,然后在第三页点击页面链接跳转到新的页面 然后在新页面点击返回按钮,返 ...
- Babel 配置用法解析
Babel 配置用法解析 刚复工的时候我司业务太多了,我已不记得我们连续作战了多少天,最近算是有时间可以学习学习我的babel大宝贝了,上周末看了下babel的一些核心模块以及babel的一些配置,今 ...
- Context与ApplicationContext的区别
ApplicationContext并没有这个类,其实更应该叫做:Activity与Application在作为Context时的区别.嗯,的确是这样的,大家在需要Context的时候,如果是在Act ...
- Python 爬虫 selenium 笔记
1. selenium 安装, 与文档 pip install selenium Selenium with Python中文翻译文档 selenium官网英文文档 2. selenium 的第一个示 ...
- vue路由传参页面刷新参数丢失问题解决方案
最近项目中涉及到跨页面传参数和后台进行数据交互,看到需求之后第一反应就是用路由传参来解决:Vue中给我们提供了三种路由传参方式,下面我们一个一个的来看一下: 方法一:params传参: this.$r ...
- 震惊,当我运行了这条Linux命令后,服务器竟然... (Linux中的删除命令)
震惊,当我运行了这条Linux命令后,服务器竟然... 0X00 写在前面 大家都听说过删库命令rm -rf /*,但是谁又真正实践过呢?但作为一个程序员,不看看这条命令执行后会发生什么,怎么能甘心呢 ...
- layer打开弹窗时传递参数(content:)
在使用layer打开弹窗时,我希望带一些参数过去,进行某些判断.直接就可以用链接+参数的方式即可. js var userGrade=Mrant layer.open({ title: '权限管理', ...
- React Native 在 Airbnb(译文)
在Android,iOS,Web和跨平台框架的横向对比中,React Native本身是一个相对较新且快速开发移动的平台.两年后,我们可以肯定地说React Native在很多方面都是革命性的.这是移 ...