0352. Data Stream as Disjoint Intervals
https://leetcode.com/problems/data-stream-as-disjoint-intervals
Description
Given a data stream input of non-negative integers a1, a2, ..., an
, summarize the numbers seen so far as a list of disjoint intervals.
Implement the SummaryRanges
class:
SummaryRanges()
Initializes the object with an empty stream.void addNum(int val)
Adds the integerval
to the stream.int[][] getIntervals()
Returns a summary of the integers in the stream currently as a list of disjoint intervals[starti, endi]
.
Example 1:
**Input**
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
**Output**
[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]
**Explanation**
SummaryRanges summaryRanges = new SummaryRanges();
summaryRanges.addNum(1); // arr = [1]
summaryRanges.getIntervals(); // return [[1, 1]]
summaryRanges.addNum(3); // arr = [1, 3]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
summaryRanges.addNum(7); // arr = [1, 3, 7]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
Constraints:
0 <= val <= 104
At most
3 * 104
calls will be made toaddNum
andgetIntervals
.
Follow up: What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?
ac
class SummaryRanges {
TreeMap<Integer, Integer> tmap;
/** Initialize your data structure here. */
public SummaryRanges() {
tmap = new TreeMap<>();
}
public void addNum(int val) {
Integer f = tmap.lowerKey(val); // floor
Integer c = tmap.ceilingKey(val); // ceil
// 4 situations
if (f != null && tmap.get(f) + 1 == val && c != null && val + 1 == c) { // concatenate both
tmap.put(f, tmap.get(c));
tmap.remove(c);
} else if (f != null && tmap.get(f) + 1 == val && (c == null || val + 1 < c)) { // concatenate floor
tmap.put(f, val);
} else if ((f == null || tmap.get(f) + 1 < val) && c != null && val + 1 == c) { // concatenate ceil
tmap.put(val, tmap.get(c));
tmap.remove(c);
} else if ((f == null || tmap.get(f) + 1 < val) && (c == null || val + 1 < c)) { // independent
tmap.put(val, val);
}
}
public List<Interval> getIntervals() {
List<Interval> res = new ArrayList<>();
for (int k : tmap.keySet()) {
res.add(new Interval(k, tmap.get(k)));
}
return res;
}
}
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/
/*
1) TreeMap, key:start, value:end; 2) when adding, check floor(val) and ceil(val); 3) 5 situations: if floor.end + 1 = val and val+1 = ceil.start, concatenate and delete ceil. concatenate both, concatenate floor, concatenate ceil, independent, already in range can't add
*/
Last updated
Was this helpful?