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. Angular总结

    angular关键核心点进行总结 1 2 angular中有很多知识点需要学习,学习成本是很大的,我通过平常开发中把一些 很重要知识点总结下来,不管是以后拿来用,或者跳槽面试需要,我都感觉是很有帮助的 ...

  2. LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...

  3. 达拉草201771010105《面向对象程序设计(java)》第三周学习总结

    达拉草201771010105«面向对象程序设计(java)»第三周学习总结 第一部分:实验部分  1.实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟 ...

  4. CSS3:TEXT-SHADOW|BOX-SHADOW(炫彩字体)

    2016年2月26日个人博客文章--迁移到segmentfault (1)text-shadow(文本阴影) 在介绍css3:text-shadow文本阴影之前,我们先来看看用它都能实现什么效果: 没 ...

  5. python读入写入中文名图片

    import os import cv2 import numpy as np # 读入中文命名图片 def cv_imread(in_path): cv_img = cv2.imdecode(np. ...

  6. 从String到==和hashcode

    public static void main(String[] args) { String s1 = "ni"; String s2 = "hao"; St ...

  7. vue+webpack工程环境搭建

    使用Vue-cli脚手架(属于vue全家桶)快速构建一个项目: [1]首先需要安装好node.js; [2]安装webpack,指令$npm install -g webpack; //如果之前有安装 ...

  8. python笔记28(TCP,UDP,socket协议)

    今日内容 1.TCP协议 协议的特点:三次握手,四次挥手: 2.UDP协议 3.OSI七层模型:每层的物理设备,每一层协议. 4.代码部分: ①介绍socket: ②使用socket完成tcp协议的w ...

  9. R|生存分析 - KM曲线 ,值得拥有姓名和颜值

    本文首发于“生信补给站”:https://mp.weixin.qq.com/s/lpkWwrLNtkLH8QA75X5STw 生存分析作为分析疾病/癌症预后的出镜频率超高的分析手段,而其结果展示的KM ...

  10. 遍历Map的四种方式(Java)

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...