zoukankan      html  css  js  c++  java
  • Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals.
    
    For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be:
    
    [1, 1]
    [1, 1], [3, 3]
    [1, 1], [3, 3], [7, 7]
    [1, 3], [7, 7]
    [1, 3], [6, 7]
    Follow up:
    What if there are lots of merges and the number of disjoint intervals are small compared to the data stream's size?

    TreeMap 解法:

    Use TreeMap to easily find the lower and higher keys, the key is the start of the interval.
    Merge the lower and higher intervals when necessary. The time complexity for adding is O(logN) since lowerKey(), higherKey(), put() and remove() are all O(logN). It would be O(N) if you use an ArrayList and remove an interval from it.

    Summary of TreeMap

    The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

    This implementation provides guaranteed log(n) time cost for the containsKeygetput and remove operations.

    methods include: ceilingKey(), floorKey(), higherKey(), lowerKey(), firstKey(): return the lowest key in the map, lastKey() return the highest key in the map

     1 /**
     2  * Definition for an interval.
     3  * public class Interval {
     4  *     int start;
     5  *     int end;
     6  *     Interval() { start = 0; end = 0; }
     7  *     Interval(int s, int e) { start = s; end = e; }
     8  * }
     9  */
    10 public class SummaryRanges {
    11     TreeMap<Integer, Interval> tree;
    12 
    13     /** Initialize your data structure here. */
    14     public SummaryRanges() {
    15         tree = new TreeMap<>();
    16     }
    17     
    18     public void addNum(int val) {
    19         if (tree.containsKey(val)) return;
    20         Integer l = tree.lowerKey(val);
    21         Integer h = tree.higherKey(val);
    22         
    23         //case 1: val is the only number between the two intervals
    24         if (l!=null && h!=null && val==tree.get(l).end+1 && val==h-1) {
    25             tree.get(l).end = tree.get(h).end;
    26             tree.remove(h);
    27         }
    28         
    29         //case 2 & 3: val is in one interval or is the next elem of that interval's last elem
    30         else if (l!=null && val<=tree.get(l).end+1) {
    31             tree.get(l).end = Math.max(tree.get(l).end, val);
    32         }
    33         
    34         //case 4: val is the first elem of a interval
    35         else if (h!=null && val==h-1) {
    36             tree.put(val, new Interval(val, tree.get(h).end));
    37             tree.remove(h);
    38         }
    39         
    40         //case 5: val does not adhere to any interval
    41         else {
    42             tree.put(val, new Interval(val, val));
    43         }
    44     }
    45     
    46     public List<Interval> getIntervals() {
    47         return new ArrayList<>(tree.values());
    48     }
    49 }
    50 
    51 /**
    52  * Your SummaryRanges object will be instantiated and called as such:
    53  * SummaryRanges obj = new SummaryRanges();
    54  * obj.addNum(val);
    55  * List<Interval> param_2 = obj.getIntervals();
    56  */

    TreeSet 解法:

     1 /**
     2  * Definition for an interval.
     3  * public class Interval {
     4  *     int start;
     5  *     int end;
     6  *     Interval() { start = 0; end = 0; }
     7  *     Interval(int s, int e) { start = s; end = e; }
     8  * }
     9  */
    10 public class SummaryRanges {
    11 
    12     /** Initialize your data structure here. */
    13     public SummaryRanges() {
    14         itvlSet = new TreeSet<Interval>(new Comparator<Interval>(){
    15             public int compare(Interval v1, Interval v2){
    16                 return v1.start-v2.start;
    17             }
    18         });
    19         
    20     }
    21     
    22     public void addNum(int val) {
    23         Interval itvl = new Interval(val,val);
    24         Interval pre = itvlSet.floor(itvl);
    25         Interval after = itvlSet.ceiling(itvl);
    26         
    27         if ( (pre!=null && pre.end >= val) || (after!=null && after.start <=val)) return;
    28         
    29         if (pre!=null && pre.end==val-1){
    30             itvlSet.remove(pre);
    31             itvl.start = pre.start;
    32         }
    33         if (after!=null && after.start==val+1){
    34             itvlSet.remove(after);
    35             itvl.end = after.end;
    36         }
    37         itvlSet.add(itvl);
    38     }
    39     
    40     public List<Interval> getIntervals() {
    41         return new ArrayList<Interval>(itvlSet);
    42         
    43     }
    44     
    45     TreeSet<Interval> itvlSet;
    46 }
    47 
    48 /**
    49  * Your SummaryRanges object will be instantiated and called as such:
    50  * SummaryRanges obj = new SummaryRanges();
    51  * obj.addNum(val);
    52  * List<Interval> param_2 = obj.getIntervals();
    53  */
  • 相关阅读:
    AngularJS指令的详解
    Linux(Ubuntu)下如何安装JDK
    Hibernate的三种状态
    JS是按值传递还是按引用传递
    git分支管理
    Hibernate注解映射联合主键的三种主要方式
    Linux下解决用户不能执行sudo的方法
    【GStreamer开发】GStreamer基础教程03——动态pipeline
    【GStreamer开发】GStreamer基础教程02——GStreamer概念
    【GStreamer开发】GStreamer基础教程02——GStreamer概念
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6100209.html
Copyright © 2011-2022 走看看