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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- React Native 学习笔记--进阶(二)--动画
React Native 进阶(二)–动画 动画 流畅.有意义的动画对于移动应用用户体验来说是非常必要的.我们可以联合使用两个互补的系统:用于全局的布局动画LayoutAnimation,和用于创建更 ...
- “代码量统计脚本”
概述 本文从一段统计C/C++程序脚本入手,记录shell脚本常用和重要的知识点. 代码量统计程序 文件名称,count_code_line.sh 12345678910111213141516171 ...
- Logback 标准xml参考
强制: [强制]应用中不可直接使用日志系统(Log4j.Logback)中的 API,而应依赖使用日志框架SLF4J 中的 API,使用门面模式的日志框架,有利于维护和各个类的日志处理方式统一.imp ...
- Daily Practice 2016-09-20
算法 回文(Palindrome)数字:判断一个数字是不是回文数字,不能使用另外的空间. 提示: 负数可以是回文数字吗? 如果转为字符串需要新分配空间 你也许想到了反转数字,但反转数字可能溢出,怎样处 ...
- sql04
1.类型转换 ),ClassId)+name from [user]; 2.一次性插入多条数据 3.日期函数 1)getdate() 返回当前日期 2)dateadd 计算增加后的时间 ,'2020- ...
- 简单说 用CSS做一个魔方旋转的效果
说明 魔方大家应该是不会陌生的,这次我们来一起用CSS实现一个魔方旋转的特效,先来看看效果图! 解释 我们要做这样的效果,重点在于怎么把6张图片,摆放成魔方的样子,而把它们摆放成魔方的样子,重点在于用 ...
- 十分钟复习CSS盒模型与BFC
css盒模型与BFC 本文为收集整理总结网上资源 旨在系统复习css盒模型与bfc 节省复习时间 阅读10分钟 什么是盒模型 每一个文档中,每个元素都被表示为一个矩形的盒子,它都会具有内容区.padd ...
- Asp.Net Core IdentityServer4 中的基本概念
一.前言 这篇文章可能大家会觉得很空洞,没有实际的实战东西,主要是自己整理出来的IdentityServer4 的一些概念性的东西:如果你对IdentityServer4有过一定的实战经验,可以跳过不 ...
- Access Token 机制详解
我们在访问很多大公司的开放 api 的时候,都会发现这些 api 要求传递一个 access token 参数.这个参数是什么呢?需要去哪里获取这个 access token 呢? access to ...
- mac 工具推荐
传送门: https://github.com/jaywcjlove/awesome-mac/blob/master/README-zh.md