Leetcode Problem 2276. Count Integers in Intervals
2276. Count Integers in Intervals
Leetcode Solutions
Using TreeMap for Interval Merging
Initialize a TreeMap to store intervals and a variable count to store the total count of unique integers.
When adding an interval, find the interval that starts before the new interval and ends after the new interval's start (if any).
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.
Insert the new or merged interval into the TreeMap.