Skip to content

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.

Hard#352
LeetCode Problem

Data Stream as Disjoint Intervals

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

Input
["SummaryRanges","addNum","addNum","getIntervals","addNum","getIntervals","addNum","getIntervals"]
[[],[4],[6],[],[5],[],[5],[]]
Output
[null,null,null,[[4,4],[6,6]],null,[[4,6]],null,[[4,6]]]
Explanation

Adding 5 bridges 4 and 6; repeating 5 changes nothing.

Constraints

0 <= value <= 10^4
At most 3 * 10^4 total calls.
At most 100 getIntervals calls.

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.