|
| 1 | +## Today's 30-03-24 [Problem Link](https://leetcode.com/problems/subarrays-with-k-different-integers/description/?envType=daily-question&envId=2024-03-30) |
| 2 | +## 992. Subarrays with K Different Integers |
| 3 | + |
| 4 | +# Intuition |
| 5 | +<!-- Describe your first thoughts on how to solve this problem. --> |
| 6 | +This problem can be solved using a sliding window approach combined with hashing. |
| 7 | +# Approach |
| 8 | +<!-- Describe your approach to solving the problem. --> |
| 9 | +- I defined a helper function `subarrayAtmostKdistinct` which calculates the number of subarrays with at most `k` distinct elements. |
| 10 | +- The main function `subarraysWithKDistinct` then calculates the number of subarrays with exactly `k` distinct elements by subtracting the count of subarrays with at most `k-1` distinct elements from the count of subarrays with at most `k` distinct elements. |
| 11 | +- I used a sliding window approach where I kept track of the frequency of elements within the current window using a hashmap. |
| 12 | +- I iterated through the array using two pointers `start` and `end`, where `start` marks the start of the current window and `end` marks the end. |
| 13 | +- I incremented `end` until the number of distinct elements within the window exceeded `k`. |
| 14 | +- At each step, I updated the hashmap and calculated the count of subarrays with at most `k` distinct elements using the formula `end - start + 1`. |
| 15 | +- I repeated this process until I reached the end of the array. |
| 16 | + |
| 17 | +By following this approach, I efficiently calculated the number of subarrays with exactly `k` distinct elements. |
| 18 | + |
| 19 | +--- |
| 20 | +Have a look at the code , still have any confusion then please let me know in the comments |
| 21 | +Keep Solving.:) |
| 22 | +# Complexity |
| 23 | +- Time complexity : $O(n)$ |
| 24 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 25 | +$n$ : length of the input array |
| 26 | +- Space complexity : $O(k)$ |
| 27 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 28 | +$k$ : given |
| 29 | +# Code |
| 30 | +``` |
| 31 | +class Solution { |
| 32 | +public static int subarraysWithKDistinct(int[] nums, int k) { |
| 33 | +
|
| 34 | +return subarrayAtmostKdistinct(nums, k) - subarrayAtmostKdistinct(nums, k-1); |
| 35 | +
|
| 36 | +} |
| 37 | +
|
| 38 | +public static int subarrayAtmostKdistinct( int[] a, int k){ // this function will give number of subarrays which have at most 'k' distinct elements |
| 39 | +int count = 0; |
| 40 | +HashMap<Integer, Integer> h = new HashMap<>(); // to store the occurences of numbers |
| 41 | +int start = 0; // to be considered when number of distinct numbers crosses 'k' |
| 42 | +for( int end = 0; end < a.length; end++){ |
| 43 | +h.put( a[end], h.getOrDefault(a[end], 0) + 1); |
| 44 | +while( h.size() > k){ |
| 45 | +int t = h.get(a[start]); // get the number of occurences of leftmost number |
| 46 | +h.put(a[start], t-1); // decrement it's number of occurence |
| 47 | +if( t-1 == 0){ // if it was the last of it's kind then remove this from hashmap |
| 48 | +h.remove(a[start]); |
| 49 | +} |
| 50 | +start++; // incremented the start |
| 51 | +} |
| 52 | +count += end - start + 1; // try to visualise with {1,2,1,3,4} with k = 3 then you will get why this (end-start+1) is added : because it will give the all subarrays with distinct numbers less than or equal to 'k' |
| 53 | +} |
| 54 | +return count; |
| 55 | +} |
| 56 | +
|
| 57 | +} |
| 58 | +``` |
0 commit comments