Leetcode Problem 2276. Count Integers in Intervals

2276. Count Integers in Intervals

Leetcode Solutions

Using TreeMap for Interval Merging

  1. Initialize a TreeMap to store intervals and a variable count to store the total count of unique integers.
  2. When adding an interval, find the interval that starts before the new interval and ends after the new interval's start (if any).
  3. Merge the new interval with any overlapping intervals, updating the count by subtracting the lengths of the removed intervals and adding the length of the new merged interval.
  4. Insert the new or merged interval into the TreeMap.
  5. When counting, simply return the count variable.
UML Thumbnail

Dynamic Segment Tree for Interval Counting

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...