LeetCode 2276: Count Integers in Intervals Solution

Master LeetCode problem 2276 (Count Integers in 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.

2276. Count Integers in Intervals

Problem Explanation

Explanation

To solve the problem, we can maintain a list of intervals in a sorted manner. When adding a new interval, we can merge it with existing intervals if there is any overlap. This approach allows us to efficiently count the number of integers present in at least one interval by iterating over the merged intervals.

Algorithm:

  1. Initialize an empty list of intervals.
  2. Implement the add method:
    • For each interval in the list:
      • If the new interval overlaps with the current interval, merge them.
      • Otherwise, keep the intervals separate.
    • After merging all possible intervals, add the new interval.
  3. Implement the count method:
    • Initialize a counter for the total count.
    • Iterate through the merged intervals and add the count of integers in each interval.
    • Return the total count.

Time Complexity:

  • Adding an interval: O(n) where n is the number of intervals.
  • Counting the total number of integers: O(n) where n is the number of intervals.

Space Complexity:

  • O(n) where n is the number of intervals.

Solution Code

class CountIntervals {
    List<int[]> intervals;
    
    public CountIntervals() {
        intervals = new ArrayList<>();
    }
    
    public void add(int left, int right) {
        List<int[]> merged = new ArrayList<>();
        boolean inserted = false;
        
        for (int[] interval : intervals) {
            if (interval[1] < left || interval[0] > right) {
                merged.add(interval);
            } else {
                left = Math.min(left, interval[0]);
                right = Math.max(right, interval[1]);
            }
        }
        
        merged.add(new int[]{left, right});
        intervals = merged;
    }
    
    public int count() {
        int totalCount = 0;
        
        for (int[] interval : intervals) {
            totalCount += interval[1] - interval[0] + 1;
        }
        
        return totalCount;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2276 (Count Integers in Intervals)?

This page provides optimized solutions for LeetCode problem 2276 (Count Integers in 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 2276 (Count Integers in Intervals)?

The time complexity for LeetCode 2276 (Count Integers in Intervals) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2276 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2276 in Java, C++, or Python.

Back to LeetCode Solutions