Practice/Meta/Leetcode 352. Data Stream as Disjoint Intervals
CodingOptional
You need to design a data structure that tracks integers from a stream and efficiently represents them as a collection of sorted, non-overlapping intervals. The structure must support two operations:
When a new number is added, it should be merged with existing intervals if it's adjacent to or overlaps with them. For example, if you have intervals [1,3] and [6,9], adding 5 would create [1,3], [5,5], [6,9], but adding 4 would merge everything into [1,9].
RangeTracker class with an empty constructoraddNum(value) that adds an integer to the trackergetIntervals() that returns a sorted list of disjoint intervalsExample 1:
` Operations: ["RangeTracker", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"] Values: [[], [1], [], [3], [], [2], []]
Output: [null, null, [[1,1]], null, [[1,1],[3,3]], null, [[1,3]]]
Explanation:
Example 2:
` Operations: ["RangeTracker", "addNum", "addNum", "addNum", "getIntervals"] Values: [[], [1], [5], [10], []]
Output: [null, null, null, null, [[1,1],[5,5],[10,10]]]
Explanation: Three non-adjacent numbers create three separate intervals `
Example 3:
` Operations: ["RangeTracker", "addNum", "addNum", "getIntervals", "addNum", "getIntervals"] Values: [[], [5], [7], [], [6], []]
Output: [null, null, null, [[5,5],[7,7]], null, [[5,7]]]
Explanation: Adding 6 bridges the gap between 5 and 7, merging them into one interval `