LeetCode 352: Data Stream as Disjoint Intervals Solution
Master LeetCode problem 352 (Data Stream as Disjoint Intervals), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
352. Data Stream as Disjoint Intervals
Problem Explanation
Explanation
To solve this problem, we can maintain a list of disjoint intervals as they are added to the stream. Whenever a new number is added, we check if it can be merged with any existing intervals. We then merge intervals accordingly and maintain the intervals in sorted order based on their start values.
Algorithm:
- Initialize an empty list to store the intervals.
- When a new number is added, iterate through the list to check if it can be merged with any existing intervals.
- If the number can be merged, update the interval accordingly.
- If the number cannot be merged with any existing interval, add a new interval to the list.
- Sort the intervals based on their start values.
Time Complexity:
- Adding a number takes O(n) time, where n is the number of intervals.
- Merging intervals and sorting the intervals take O(nlogn) time.
- Overall, the time complexity is O(nlogn).
Space Complexity:
- The space complexity is O(n) to store the intervals.
Solution Code
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class SummaryRanges {
List<int[]> intervals;
public SummaryRanges() {
intervals = new ArrayList<>();
}
public void addNum(int val) {
int[] newInterval = {val, val};
List<int[]> newIntervals = new ArrayList<>();
boolean inserted = false;
for (int[] interval : intervals) {
if (interval[1] + 1 < newInterval[0]) {
newIntervals.add(interval);
} else if (interval[0] - 1 > newInterval[1]) {
if (!inserted) {
newIntervals.add(newInterval);
inserted = true;
}
newIntervals.add(interval);
newInterval = interval;
} else {
newInterval[0] = Math.min(newInterval[0], interval[0]);
newInterval[1] = Math.max(newInterval[1], interval[1]);
}
}
if (!inserted) {
newIntervals.add(newInterval);
}
intervals = newIntervals;
}
public int[][] getIntervals() {
return intervals.toArray(new int[intervals.size()][]);
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 352 (Data Stream as Disjoint Intervals)?
This page provides optimized solutions for LeetCode problem 352 (Data Stream as Disjoint Intervals) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 352 (Data Stream as Disjoint Intervals)?
The time complexity for LeetCode 352 (Data Stream as Disjoint Intervals) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 352 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 352 in Java, C++, or Python.