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的更多相关文章

  1. [LeetCode] Falling Squares 下落的方块

    On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...

  2. [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 ...

  3. LeetCode699. Falling Squares

    On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...

  4. 699. Falling Squares

    On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...

  5. 【leetcode】699. Falling Squares

    题目如下: On an infinite number line (x-axis), we drop given squares in the order they are given. The i- ...

  6. leetcode 699. Falling Squares 线段树的实现

    线段树实现.很多细节值得品味 都在注释里面了 class SegTree: def __init__(self,N,query_fn,update_fn): self.tree=[0]*(2*N+2) ...

  7. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. 美团新零售招聘-高级测试开发(20k-50k/月)

    内推邮箱:liuxinguang@meituan.com 地点:北京 职位级别:p2-2以上级别 15.5薪

  2. linux下查找文件及查找包含指定内容的文件常用命令

    whereis <程序名称> 查找软件的安装路径-b 只查找二进制文件-m 只查找帮助文件-s 只查找源代码-u 排除指定类型文件-f 只显示文件名-B <目录> 在指定目录下 ...

  3. SQL注入攻击浅谈

    原理 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作,其主要原因是程序没有细致地过滤用户输入的数据 ...

  4. HelloWorld系列(一)- 手把手教你做JDK环境变量配置

    分下载,配置,验证三个步骤讲解如何进行JDK环境变量配置. 步骤1:首先看配置成功后的效果步骤2:下载,并解压到E:\JDK步骤3:环境变量配置步骤4:Win10 下环境变量Path的配置步骤5:验证 ...

  5. idea导入 spring framework项目

    准备的环境:gradle,idea 注意:gradle版本不一致会报各种错误,那么怎么查找依赖的版本呢? 首先在git上把spring framework项目拉取下来, 步骤一:复制URL路径 步骤二 ...

  6. MVC09

    1.委托(delegate)调用静态方法 委托类似于C++中的函数指针. 某方法仅仅在执行的时候才能确定是否被调用. 是实现事件和回调函数的基础. 面向对象,安全性高. using System; u ...

  7. Ubuntu pppoe宽带拨号相关问题

    因为可视化界面没有相关设置,因此采用终端命令的方法. 测试环境:Ubuntu 18.0.4 pppoe的配置:$ sudo pppoeconf 然后进入此界面进行一系列宽带拨号的设置. 联网:$ su ...

  8. 初识 “HTML”

    HTML 什么是HTML? ①全称:超文本标记语言②超文本:在普通的文本内容的基础上添加超链接.图片.视频等③标记语言:HTML提供一系列标签④版本:HTML 4.01 HTML声明 1.编码格式:H ...

  9. 使用nestjs集成grpc具体操作

    两个程序中, 提供grpc服务的称为服务端, 调用grpc服务的为客户端, 以下是grpc服务端和客户端的代码编写     1. 创建两个nestjs项目demo1(端口: 3000)和demo2(端 ...

  10. Flutter 拖拽排序组件 ReorderableListView

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 ReorderableListView是通过长按拖动某一项 ...