2276. Count Integers in Intervals
2276. Count Integers in Intervals
Description
Given an empty set of intervals, implement a data structure that can:
- Add an interval to the set of intervals.
- Count the number of integers that are present in at least one interval.
Implement the CountIntervals class:
CountIntervals()Initializes the object with an empty set of intervals.void add(int left, int right)Adds the interval[left, right]to the set of intervals.int count()Returns the number of integers that are present in at least one interval.
Note that an interval [left, right] denotes all the integers x where left <= x <= right.
Example 1:
1 | Input |
Constraints:
1 <= left <= right <= 10^9- At most
10^5calls in total will be made toaddandcount. - At least one call will be made to
count.
Hints/Notes
- 2025/04/12 Q1
- merge intervals
- 0x3Fâs solution
Solution
Language: C++
1 | class CountIntervals { |