Practice Data Stream as Disjoint Intervals
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
SummaryRanges starts empty. addNum(value) records an integer, and getIntervals() returns sorted inclusive intervals covering exactly the distinct integers seen so far. Adjacent integers belong in one interval; repeating a value does not change the summary. Practice approach: Store intervals ordered by their start. On insertion, inspect neighboring intervals and either extend one, merge two, create a singleton, or do nothing for an already-covered value. The bounded domain also permits a presence array followed by a scan. Test a value that bridges two intervals.
Source: Data Stream as Disjoint Intervals on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
["SummaryRanges","addNum","addNum","getIntervals","addNum","getIntervals","addNum","getIntervals"] [[],[4],[6],[],[5],[],[5],[]]
[null,null,null,[[4,4],[6,6]],null,[[4,6]],null,[[4,6]]]
Adding 5 bridges 4 and 6; repeating 5 changes nothing.
Constraints
Related LeetCode Problems
Practice more problems similar to Data Stream as Disjoint Intervals.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.