zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    

      

  • 相关阅读:
    给VPS装桌面
    GIT免密码PUSH
    验证码类
    Url几个常用的函数
    PHP--关于模板的原理和解析
    管理员权限执行批处理和静默执行regsvr32注册
    linux 维护常见场景小命令 (未完待续)
    批处理定时重启print打印服务,解决打印机异常队列堆积
    Linux----LVM扩容磁盘空间
    6、Samba 服务器配置
  • 原文地址:https://www.cnblogs.com/hyserendipity/p/12165204.html
Copyright © 2011-2022 走看看