<jupyter_start><jupyter_text>Sub problem: find unique subsets in a list which have k distinct intergers https://leetcode.com/problems/subarrays-with-k-different-integers/submissions/<jupyter_code>from collections import defaultdict, Counter
from typing import List
class Solution:
    def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
        def helper(k):
            """
            counts all the subsets which have at most k unique intergers
            """
            counts = defaultdict(int)
            start = 0
            res = 0
            for end, rn in enumerate(A):
                counts[rn] += 1
                
                while len(counts) > k:
                    ln = A[start]
                    counts[ln] -= 1
                    if counts[ln] == 0:
                        del counts[ln]
                    start += 1
                # this is the trickiest part. we add the window_length at the end of each loop to our answer
                res += end - start + 1
            return res
        # here we are saying that if we subtract the at_most calcs for K and K-1, we end up with the the # of unique subsets at K
        return helper(K) - helper(K-1)<jupyter_output><empty_output><jupyter_text>bonus: find  of subsets with atleast K distinct elements<jupyter_code>def atleastK(A, K):
    counts = defaultdict(int)
    start, res = 0,0
    for end, rn in enumerate(A):
        counts[rn] += 1
        
        while len(counts) >= K:
            ln = A[start]
            counts[ln] -= 1
            if counts[ln] == 0:
                del counts[ln]
            start += 1
        
    return res
atleastK([1,2,1,2,3], 2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08856304985337243, 'fraction_numerical': 0.012316715542521995, 'mean_word_length': 1.6908517350157728, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '597432', 'license': 'MIT', 'n_tokens_mistral': 525, 'n_tokens_neox': 488, 'n_words': 164, 'path': 'lt992_subarray_w_k_different_integers_sliding_window.ipynb', 'repo_name': 'devkosal/code_challenges', 'size': '1705'}