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  */
时间: 2024-10-25 15:50:43

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

352[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

[email&#160;protected] [352] Data Stream as Disjoint Intervals (Binary Search &amp; TreeSet)

https://leetcode.com/problems/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 da

352. 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. 解题思路: 这道题是目前最新的题,其实思路很容易找到,难点在于考虑到所有的可能的情形. 首先要确定类必须有一个保存当前结果的集合类List<Interval>,其元素的按Interval的起始值的大小排序,

352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)

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

[leetcode]352. Data Stream as Disjoint Intervals

数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样提交了,发现错了,想到之前考虑要不要判重,我感觉是这个问题,然后就是在二分查找的时候,判断一下左右区间是否包含当前的值,包含就直接返回. 1 /** 2 * Definition for an interval. 3 * struct Interval { 4 * int start; 5 * in

352. Data Stream as Disjoint Interval

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

[LeetCode] Find Median from Data Stream 找出数据流的中位数

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Design a d

leetcode笔记:Find Median from Data Stream

一. 题目描述 Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 De