3

I am trying to solve an algorithm problem:

Given an array nums of positive integers, call a (contiguous, not necessarily distinct) subarray of nums "good" if the number of different integers in that subarray is exactly k. For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3. Return the number of good subarrays of nums.

Looking at the solution here, I was able to come up with the following sliding window approach:

class Solution {
public:
    int helper(vector<int>& nums, int B) {
        unordered_map<int, int> m;

        int start=0, end=0, count=0;
        while(end<nums.size()) {
            m[nums[end]]++;
            while(m.size()>B) {
                m[nums[start]]--;
                if(m[nums[start]]<=0) m.erase(nums[start]);
                start++;
            }
            count+=(end-start+1);
            end++;
        }
        return count;
    }
    
    int subarraysWithKDistinct(vector<int>& nums, int k) {
        return helper(nums, k)-helper(nums, k-1);
    }
};

I understand how the helper() function works, but not the intuition behind calculating number of subarrays with atmost k and k-1 distinct integers and subtracting them (i.e., behind helper(nums, k)-helper(nums, k-1)) to get the number of subarrays with exactly equal to k distinct integers.

What understanding am I missing?

halfer
  • 19,824
  • 17
  • 99
  • 186
J. Doe
  • 1,291
  • 2
  • 10
  • 19
  • The closest thing I [found](https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/523136/JavaC++Python-Sliding-Window/486014) is the statement: "Generally, the sliding window problems have some kind of aggregate, atMost k, largest substring, min substring with k etc. They're always "given an array or string, find some computed sub problem" value." – J. Doe Jul 21 '21 at 04:56

1 Answers1

2

I understand how the helper() function works, but not the intuition behind calculating number of subarrays with atmost k and k-1 distinct integers and subtracting them (i.e., behind helper(nums, k)-helper(nums, k-1)) to get the number of subarrays with exactly equal to k distinct integers.

Well, suppose you have a room with a bunch of children in it. It turns out that there are 12 children who are five years old or under, and 8 children who are four years old or under.

Now presumably you agree that each one of the "children four years old or under" is also one of the "children five years old or under," right?

So suppose we take the 12 children five years old or younger, and remove from them the 8 children four years old or younger. Of course, 12 - 8 = 4 children remain.

Now, how old is each of these children?

Daniel McLaury
  • 4,047
  • 1
  • 15
  • 37
  • 1
    Oh, I understand _that_; what I don't understand is the _need/intuition_ behind doing it that way. Why couldn't we directly find the result for **_exactly equal to k_** distinct integers? – J. Doe Jul 21 '21 at 13:55
  • 1
    Well, the reason to do things this way would be because you see a substantially cheaper way to count subsets with _at most_ k distinct elements than to count subsets with _exactly_ k elements. – Daniel McLaury Jul 21 '21 at 14:32