C# O(N) dictionary, two pointers
\n\t\tpublic int SubarraysWithKDistinct(int[] A, int K) {\n            int res = 0;\n            Dictionary<int, int> dict = new Dictionary<int, int>();\n      
```\n\t\tpublic int SubarraysWithKDistinct(int[] A, int K) {\n            int res = 0;\n            Dictionary<int, int> dict = new Dictionary<int, int>();\n            int start = 0;\n            int current = 0;\n            for(int i = 0; i < A.Length; i++) {\n                if(dict.ContainsKey(A[i])) {\n                    dict[A[i]]++;\n                } else {\n                    dict[A[i]] = 1;\n                }\n                if(dict.Keys.Count >= K) {\n                    while(dict[A[current]] > 1) {\n                        dict[A[current++]]--;    \n                    }\n                    if (dict.Keys.Count == K) {\n                        res += current - start + 1;\n                    } else {\n                        dict.Remove(A[current++]);\n                        start = current;\n                        dict[A[i]] = 0;\n                        i--;\n                    }\n                } \n            }\n            return res;\n        }\n```
[]