From a1dc91d969a7c0c81ead1f2e8c84162a405e1547 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 30 Mar 2024 23:03:35 +0530 Subject: [PATCH] Create 992. Subarrays with K Different Integers --- 992. Subarrays with K Different Integers | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 992. Subarrays with K Different Integers diff --git a/992. Subarrays with K Different Integers b/992. Subarrays with K Different Integers new file mode 100644 index 0000000..c410e11 --- /dev/null +++ b/992. Subarrays with K Different Integers @@ -0,0 +1,37 @@ +class Solution { +public: + int findAtMostK(int n, int k, vector&nums){ + int ans = 0; + int left = 0, right = 0; + unordered_mapmp; + while(right k && left<=right){ + mp[nums[left]]--; + if(mp[nums[left]]==0){ + mp.erase(nums[left]); + } + left++; + } + ans += right-left+1; + right++; + } + return ans; + } + int subarraysWithKDistinct(vector& nums, int k) { + int n = nums.size(); + return findAtMostK(n, k, nums) - findAtMostK(n, k-1, nums); + } +}; +auto init = [](){ + ios::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + return 'c'; +}();