Easiest ✅✅ Python3 / C++ / Java / Python 🐍/C ✅🔥🔥 Explained 💀☠️Beats 100%
Intuition\n\nC++ []\nclass Solution {\n    int helper(vector<int>& nums, int k) {\n        unordered_map<int, int> mp;\n        int l = 0, res = 0;\n        for
# Intuition\n![Screenshot 2024-03-30 074930.png](https://assets.leetcode.com/users/images/6fea1259-5b89-49fd-ad31-cccb488b44c2_1711765231.1563656.png)\n```C++ []\nclass Solution {\n    int helper(vector<int>& nums, int k) {\n        unordered_map<int, int> mp;\n        int l = 0, res = 0;\n        for (int r = 0; r < nums.size(); r++) {\n            if (!mp[nums[r]]++)\n                k--;\n            while (k < 0)\n                if (!--mp[nums[l++]])\n                    k++;\n            res += r - l + 1;\n        }\n        return res;\n    }\n\npublic:\n    int subarraysWithKDistinct(vector<int>& nums, int k) {\n        return helper(nums, k) - helper(nums, k - 1);\n    }\n};\n```\n```python3 []\nclass Solution:\n    def subarraysWithKDistinct(self, A: List[int], K: int) -> int:\n        return self.atMostK(A, K) - self.atMostK(A, K - 1)\n    def atMostK(self, A, K):\n        count = collections.Counter()\n        res = i = 0\n        for j in range(len(A)):\n            if count[A[j]] == 0: K -= 1\n            count[A[j]] += 1\n            while K < 0:\n                count[A[i]] -= 1\n                if count[A[i]] == 0: K += 1\n                i += 1\n            res += j - i + 1\n        return res\n```\n```Java []\nclass Solution {\n    private int helper(int[] nums, int k) {\n        HashMap<Integer, Integer> mp = new HashMap<>();\n        int l = 0, res = 0;\n        for (int r = 0; r < nums.length; r++) {\n            if (mp.getOrDefault(nums[r], 0) == 0)\n                k--;\n            mp.put(nums[r], mp.getOrDefault(nums[r], 0) + 1);\n            while (k < 0) {\n                mp.put(nums[l], mp.get(nums[l]) - 1);\n                if (mp.get(nums[l++]) == 0)\n                    k++;\n            }\n            res += r - l + 1;\n        }\n        return res;\n    }\n\n    public int subarraysWithKDistinct(int[] nums, int k) {\n        return helper(nums, k) - helper(nums, k - 1);\n    }\n}\n```\n```Python []\nclass Solution(object):\n    def subarraysWithKDistinct(self, nums, k):\n        """\n        :type nums: List[int]\n        :type k: int\n        :rtype: int\n        """\n        cnt, res, l, m = [0] * (len(nums) + 1), 0, 0, 0\n        for n in nums:\n            cnt[n] += 1\n            if cnt[n] == 1:\n                k -= 1\n                if (k < 0):\n                    cnt[nums[m]] = 0\n                    m += 1\n                    l = m\n            if k <= 0:\n                while(cnt[nums[m]] > 1):\n                    cnt[nums[m]] -= 1\n                    m += 1\n                res += m - l + 1\n        return res  \n```\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(1)\n# Code\n```\nclass Solution(object):\n    def subarraysWithKDistinct(self, nums, k):\n        """\n        :type nums: List[int]\n        :type k: int\n        :rtype: int\n        """\n        cnt, res, l, m = [0] * (len(nums) + 1), 0, 0, 0\n        for n in nums:\n            cnt[n] += 1\n            if cnt[n] == 1:\n                k -= 1\n                if (k < 0):\n                    cnt[nums[m]] = 0\n                    m += 1\n                    l = m\n            if k <= 0:\n                while(cnt[nums[m]] > 1):\n                    cnt[nums[m]] -= 1\n                    m += 1\n                res += m - l + 1\n        return res  \n```\n# Please upvote![th.jpeg](https://assets.leetcode.com/users/images/75dc7e42-b284-47f3-8e57-da11ee1c3d34_1711765375.7033033.jpeg)\n
['Array', 'Hash Table', 'C', 'Sliding Window', 'Python', 'C++', 'Java', 'Python3']