bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 352. Data Stream as Disjoint Intervals

352. Data Stream as Disjoint Intervals

Leetcode Solutions

Maintain Disjoint Intervals with TreeMap

Algorithm

  1. Initialize a TreeMap to store intervals.
  2. When addNum is called: a. Check if the number merges with the interval to its left or right. b. If it merges with both, remove the right interval and update the left interval to cover both. c. If it merges with one, update the corresponding interval. d. If it doesn't merge, insert it as a new interval.
  3. When getIntervals is called, return all intervals in the TreeMap as a list.
UML Thumbnail

Save All Values in an Ordered Set

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...