首页 > 代码库 > Leetcode: Data Stream as Disjoint Intervals
Leetcode: Data Stream as Disjoint Intervals
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.
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 */
Leetcode: Data Stream as Disjoint Intervals
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。