• 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);
        }
    }
    

      

  • 相关阅读:
    『设计模式』再谈麦当劳的点单模式--命令模式(Command)
    『设计模式』备忘录模式(memento)下象棋,我就想悔棋怎么办
    『设计模式』职责链模式(Chain of Responsibility) 可怜的加薪、请假之路
    『设计模式』状态模式(不起花里胡哨的名字了)
    『设计模式』外观模式--这篇博客也太明了吧
    『设计模式』电话接线员与中介者模式
    『安卓』安卓开发基础--基本控件
    『设计模式』再谈Macdonald的汉堡口味--策略模式
    hive启动报错:Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgumen
    hive安装报错:Class path contains multiple SLF4J bindings
  • 原文地址:https://www.cnblogs.com/hyserendipity/p/12165204.html
Copyright © 2020-2023  润新知