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. OPPO招聘-互联网测试

    邮       箱:ljy@oppo.com 工作地点:深圳

  2. Linux sed命令实例解析

    最近看project的makefile,又见到了sed的强大编辑能力,在makefile工作之前,通常都是执行脚本或者make menuconfig来配置好各种全局变量.sed活动阶段通常在bash ...

  3. Android apk签名详解——AS签名、获取签名信息、系统签名、命令行签名

    Apk签名,每一个Android开发者都不陌生.它就是对我们的apk加了一个校验参数,防止apk被掉包.一开始做Android开发,就接触到了apk签名:后来在微信开放平台.高德地图等平台注册时,需要 ...

  4. jquery.qrcode笔记

    由于一个坑爹的项目需要生成二维码扫描,后台由于数据库比较麻烦,就只能前端做了,于是乎找到Js生成qrcode的一个库:https://github.com/jeromeetienne/jquery-q ...

  5. C++走向远洋——63(项目二2、两个成员的类模板)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  6. Intellij IDEA 干货分享

    更多视频详情:https://www.bilibili.com/video/av89385013/ Intellij IDEA 真是越用越强大 它总是在我们写代码的时候 不时给我们来个小惊喜 出于对 ...

  7. Tornado 简述

    前言 python 旗下,群英荟萃,豪杰并起.单是用于 web 开发的,就有 webpy.web2py.bottle.pyramid.zope2.flask.tornado.django 等等,不一而 ...

  8. Python——详解collections工具库

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天为大家介绍Python当中一个很好用也是很基础的工具库,叫做collections. collection在英文当中有容器的意思,所以顾 ...

  9. Python左手画条龙右手画个彩虹

    左手画龙右手画彩虹听说很火,Python也可以画出很美的彩虹,准确的说像彩虹棒棒糖:) 效果如下图: # -*- coding: utf-8 -*- # @Time : 2019/12/16 23:2 ...

  10. SpringBoot入门系列(二)如何返回统一的数据格式

    前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...