Similar Problems

Similar Problems not available

Count Integers In Intervals - Leetcode Solution

Companies:

LeetCode:  Count Integers In Intervals Leetcode Solution

Difficulty: Hard

Topics: design  

Problem Statement:

You are given an array of n non-negative integers nums, a 2D array queries where queries[i] = [lefti, righti] denotes an interval, please count the number of distinct numbers in the subarrays specified by all the queries and return an array of results for all queries.

Example:

Input: nums = [1,3,4,2,2,8], queries = [[0,2],[2,3],[1,4],[2,5]] Output: [3,2,4,5] Explanation: The queries are processed as follows:

  • Query [0,2] creates the subarray [1,3,4]. There are 3 distinct numbers in this subarray: 1, 3, and 4.
  • Query [2,3] creates the subarray [4, 2]. There are 2 distinct numbers in this subarray: 2 and 4.
  • Query [1,4] creates the subarray [3,4,2,2]. There are 4 distinct numbers in this subarray: 2, 3, 4, and 8.
  • Query [2,5] creates the subarray [4,2,2,8]. There are 5 distinct numbers in this subarray: 2, 4, 8, 4, and 2 (the number 2 is counted twice because it appears twice in the subarray).

Solution:

Approach: To solve this problem, we can use a hash table to keep track of the count of the number of times a number appears. Then, we can iterate over the queries, and for each query, we can iterate over the subarray specified by the query and count the number of distinct numbers in that subarray.

Algorithm:

  1. Create a hash table to keep track of the count of the number of times a number appears in the array.

  2. Iterate over the array nums and update the count of each number in the hash table.

  3. For each query [left, right], create a hash table to keep track of the count of the number of times a number appears in the subarray specified by the query.

  4. Iterate over the subarray specified by the query and update the count of each number in the hash table.

  5. Count the number of distinct numbers in the hash table and add it to the result array.

  6. Return the result array.

Time Complexity: The time complexity of this algorithm is O(nq), where n is the length of the input array nums and q is the number of queries.

Space Complexity: The space complexity of this algorithm is O(n), where n is the length of the input array nums. This is because we need to store the count of each number in the hash table.

Code:

class Solution { public: vector<int> distinctNumbers(vector<int>& nums, vector<vector<int>>& queries) { vector<int> res; unordered_map<int, int> counts;

    // Count the number of times each number appears in the input array
    for (int num : nums) {
        counts[num]++;
    }
    
    // Iterate over the queries and count the number of distinct numbers in each subarray
    for (vector<int> query : queries) {
        unordered_map<int, int> sub_counts;
        int left = query[0];
        int right = query[1];
        
        // Count the number of times each number appears in the subarray specified by the query
        for (int i = left; i <= right; i++) {
            sub_counts[nums[i]]++;
        }
        
        int distinct = 0;
        
        // Count the number of distinct numbers in the subarray
        for (auto& [num, count] : sub_counts) {
            if (counts[num] >= count) {
                distinct++;
            }
        }
        
        res.push_back(distinct);
    }
    
    return res;
}

};

Count Integers In Intervals Solution Code

1