<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>good subarrays</strong> of </em><code>nums</code>.</p>

<p>A <strong>good array</strong> is an array where the number of different integers in that array is exactly <code>k</code>.</p>

<ul>
	<li>For example, <code>[1,2,3,1,2]</code> has <code>3</code> different integers: <code>1</code>, <code>2</code>, and <code>3</code>.</li>
</ul>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,1,2,3], k = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,1,3,4], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i], k &lt;= nums.length</code></li>
</ul>

template<typename T>
void print_container(T &t){
    cout<<t;
}

template<typename ...T,template<typename...> typename Tuple>
void print_container(Tuple<T...> &t){
    cout<<'(';
    [&t]<size_t ...I>(index_sequence<I...>){
        (...,(cout<<(I==0 ? "":",")<<get<I>(t)));
    }(make_index_sequence<sizeof...(T)>{});
    cout<<')';
}

template<typename T,template<typename...> typename C>
void print_container(C<T>& c){
    for(auto &v:c){
        print_container(v);
        cout<<',';
    }
    cout<<endl;
}

template<typename K,typename V>
void print_container(unordered_map<K,V>& c){
    for(auto &v:c){
        print_container(v);
        cout<<',';
    }
    cout<<endl;
}


class Solution {
public:
    auto smallest_next_windows(vector<int>& nums,int k)->vector<int>{
        auto l=0,r=0;
        auto window_count=unordered_map<int,int>{{nums[r],1}};
        auto window_ends=vector<int>(nums.size(),-1);
        while(r<nums.size()){
            if(window_count.size()<k){
                r++;
                if(r<nums.size()){
                    window_count[nums[r]]++;
                }
            }
            else{
                while(window_count.size()==k){
                    window_ends[l]=r;
                    if(--window_count[nums[l]]==0){
                        window_count.erase(nums[l]);
                    }
                    l++;
                }
            }
        }

        return window_ends;
    }

    int subarraysWithKDistinct(vector<int>& nums, int k) {
        auto res=0;
        auto l=0,r=0;
        auto window_count=unordered_map<int,int>{};
        auto prev_val_index=unordered_map<int,int>{};
        auto next_val_index=vector<int>(nums.size(),-1);
        auto smallest_window_end=smallest_next_windows(nums,k);
        
        while(r<nums.size()){
            while(r<nums.size()&&window_count.size()<k){
                if(prev_val_index.count(nums[r])!=0){
                    next_val_index[prev_val_index[nums[r]]]=r;
                }
                prev_val_index[nums[r]]=r;
                window_count[nums[r]]++;
                r++;
            }

            while(r<nums.size()&&window_count.size()==k){
                res++;
                if(window_count.count(nums[r])==0){
                    break;
                }
                if(prev_val_index.count(nums[r])!=0){
                    next_val_index[prev_val_index[nums[r]]]=r;
                }
                prev_val_index[nums[r]]=r;

                window_count[nums[r]]++;
                r++;
            }
            if(r==nums.size()&&window_count.size()==k){
                res++;
            }

            while(window_count.size()==k){
                if(min(smallest_window_end[l],next_val_index[l])!=-1){
                    res+=r-max(smallest_window_end[l],next_val_index[l]);
                }
                if(--window_count[nums[l]]==0){
                    window_count.erase(nums[l]);
                }
                l++;
            }
        }
        
        return res;
    }
};
64356