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  */
  • 相关阅读:
    android控件
    解决C#项目出现“此项目引用这台计算机上缺少的 NuGet 程序包。使用 NuGet 程序包还原可下载这些程序包。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 ..packagesMicrosoft.Net.Compilers.1.0.0uildMicrosoft.Net.Compilers.props”
    C#解密退款req_info结果通知
    解决C#中调用WCF方法报错:远程服务器返回错误 (404) 未找到
    解决网页出现 net::ERR_ABORTED 404 (Not Found)问题
    winform执行程序报错:已停止工作,windows正在检查该问题的解决方案
    C#操作数据表中XML格式的数据
    为了开篇
    iOS隐私政策
    在Android中调用KSOAP2库访问webservice服务出现的服务端传入参数为null的问题解决
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6100209.html
Copyright © 2011-2022 走看看