2020-01-08 10:16:37

一、Falling squares

问题描述:

问题求解:

本题其实也是一条经典的区间问题,对于区间问题,往往可以使用map来进行区间的维护操作。

  1. class Interval {
  2. int start;
  3. int end;
  4. int height;
  5.  
  6. public Interval(int start, int end, int height) {
  7. this.start = start;
  8. this.end = end;
  9. this.height = height;
  10. }
  11. }
  12.  
  13. public List<Integer> fallingSquares(int[][] positions) {
  14. List<Integer> res = new ArrayList<>();
  15. List<Interval> record = new ArrayList<>();
  16. int max_height = Integer.MIN_VALUE;
  17. for (int[] p : positions) {
  18. int s = p[0];
  19. int e = p[0] + p[1];
  20. int h = p[1];
  21. int curr_max = 0;
  22. for (Interval inte : record) {
  23. if (inte.start >= e || inte.end <= s) continue;
  24. curr_max = Math.max(curr_max, inte.height);
  25. }
  26. h += curr_max;
  27. record.add(new Interval(s, e, h));
  28. max_height = Math.max(max_height, h);
  29. res.add(max_height);
  30. }
  31. return res;
  32. }

  

二、Range module

问题描述:

问题求解:

Range module和上题都可以采用map来进行区间维护得到最终的解,时间复杂度也同样是O(n ^ 2)。

  1. class RangeModule {
  2. class Interval {
  3. int start;
  4. int end;
  5. boolean is_tracked;
  6.  
  7. public Interval(int start, int end, boolean is_tracked) {
  8. this.start = start;
  9. this.end = end;
  10. this.is_tracked = is_tracked;
  11. }
  12. }
  13.  
  14. List<Interval> record;
  15.  
  16. public RangeModule() {
  17. record = new ArrayList<>();
  18. }
  19.  
  20. public void addRange(int s, int e) {
  21. List<Interval> del = new ArrayList<>();
  22. List<Interval> add = new ArrayList<>();
  23. for (Interval inte : record) {
  24. if (inte.start >= e || inte.end <= s) continue;
  25. del.add(inte);
  26. if (s <= inte.start && e >= inte.end) continue;
  27. else if (s <= inte.start) add.add(new Interval(e, inte.end, true));
  28. else if (e >= inte.end) add.add(new Interval(inte.start, s, true));
  29. else {
  30. add.add(new Interval(inte.start, s, true));
  31. add.add(new Interval(e, inte.end, true));
  32. }
  33. }
  34. for (Interval inte : del) record.remove(inte);
  35. for (Interval inte : add) record.add(inte);
  36. record.add(new Interval(s, e, true));
  37. }
  38.  
  39. public boolean queryRange(int s, int e) {
  40. int target = e - s;
  41. int curr = 0;
  42. for (Interval inte : record) {
  43. if (inte.start >= e || inte.end <= s) continue;
  44. int l = Math.max(inte.start, s);
  45. int r = Math.min(inte.end, e);
  46. curr += r - l;
  47. }
  48. return curr == target;
  49. }
  50.  
  51. public void removeRange(int s, int e) {
  52. List<Interval> del = new ArrayList<>();
  53. List<Interval> add = new ArrayList<>();
  54. for (Interval inte : record) {
  55. if (inte.start >= e || inte.end <= s) continue;
  56. del.add(inte);
  57. if (s <= inte.start && e >= inte.end) continue;
  58. else if (s <= inte.start) add.add(new Interval(e, inte.end, true));
  59. else if (e >= inte.end) add.add(new Interval(inte.start, s, true));
  60. else {
  61. add.add(new Interval(inte.start, s, true));
  62. add.add(new Interval(e, inte.end, true));
  63. }
  64. }
  65. for (Interval inte : del) record.remove(inte);
  66. for (Interval inte : add) record.add(inte);
  67. }
  68. }

  

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. React Native 学习笔记--进阶(二)--动画

    React Native 进阶(二)–动画 动画 流畅.有意义的动画对于移动应用用户体验来说是非常必要的.我们可以联合使用两个互补的系统:用于全局的布局动画LayoutAnimation,和用于创建更 ...

  2. “代码量统计脚本”

    概述 本文从一段统计C/C++程序脚本入手,记录shell脚本常用和重要的知识点. 代码量统计程序 文件名称,count_code_line.sh 12345678910111213141516171 ...

  3. Logback 标准xml参考

    强制: [强制]应用中不可直接使用日志系统(Log4j.Logback)中的 API,而应依赖使用日志框架SLF4J 中的 API,使用门面模式的日志框架,有利于维护和各个类的日志处理方式统一.imp ...

  4. Daily Practice 2016-09-20

    算法 回文(Palindrome)数字:判断一个数字是不是回文数字,不能使用另外的空间. 提示: 负数可以是回文数字吗? 如果转为字符串需要新分配空间 你也许想到了反转数字,但反转数字可能溢出,怎样处 ...

  5. sql04

    1.类型转换 ),ClassId)+name from [user]; 2.一次性插入多条数据 3.日期函数 1)getdate() 返回当前日期 2)dateadd 计算增加后的时间 ,'2020- ...

  6. 简单说 用CSS做一个魔方旋转的效果

    说明 魔方大家应该是不会陌生的,这次我们来一起用CSS实现一个魔方旋转的特效,先来看看效果图! 解释 我们要做这样的效果,重点在于怎么把6张图片,摆放成魔方的样子,而把它们摆放成魔方的样子,重点在于用 ...

  7. 十分钟复习CSS盒模型与BFC

    css盒模型与BFC 本文为收集整理总结网上资源 旨在系统复习css盒模型与bfc 节省复习时间 阅读10分钟 什么是盒模型 每一个文档中,每个元素都被表示为一个矩形的盒子,它都会具有内容区.padd ...

  8. Asp.Net Core IdentityServer4 中的基本概念

    一.前言 这篇文章可能大家会觉得很空洞,没有实际的实战东西,主要是自己整理出来的IdentityServer4 的一些概念性的东西:如果你对IdentityServer4有过一定的实战经验,可以跳过不 ...

  9. Access Token 机制详解

    我们在访问很多大公司的开放 api 的时候,都会发现这些 api 要求传递一个 access token 参数.这个参数是什么呢?需要去哪里获取这个 access token 呢? access to ...

  10. mac 工具推荐

    传送门: https://github.com/jaywcjlove/awesome-mac/blob/master/README-zh.md