Subarrays with K Different Integers | Approach-2 | One Pass | Leetcode 992 | codestorywithMIK
Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
This is the 20th Video of our Playlist "Sliding Window : Popular Interview Problems".
In this video we will try to solve an very classic sliding window problem :
Subarrays with K Different Integers | Approach-2 | One Pass | Leetcode 992 | codestorywithMIK
Approach-1 Video - https://www.youtube.com/watch?v=uJJSGxfzix8
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Subarrays with K Different Integers | Approach-2 | One Pass | Leetcode 992 | codestorywithMIK
Company Tags : Expedia, Uber, Amazon
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Sliding%20Window/Subarrays%20with%20K%20Different%20Integers.cpp
Leetcode Link : https://leetcode.com/problems/subarrays-with-k-different-integers/
My DP Concepts Playlist : https://youtu.be/7eLMOE1jnls
My Graph Concepts Playlist : https://youtu.be/5JGiZnr6B5w
My Recursion Concepts Playlist : https://www.youtube.com/watch?v=pfb1Zduesi8&list=PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM
My Sliding Window Playlist : https://www.youtube.com/watch?v=mrUBUWb23hk&list=PLpIkg8OmuX-J2Ivo9YdY7bRDstPPTVGvN&index=1
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Interview_DS_Algo
Instagram : https://www.instagram.com/codestorywithmik/
Facebook : https://www.facebook.com/people/codestorywithmik/100090524295846/
Twitter : https://twitter.com/CSwithMIK
Subscribe to my channel : https://www.youtube.com/@codestorywithMIK
Approach Summary :
Sure, here's a summary of the approach implemented in the provided Java code:
The algorithm aims to find the number of subarrays within a given array (`nums`) that contain exactly `k` distinct elements. Here's a breakdown of the approach:
1. **Initialization**:
- Initialize a HashMap `mp` to store the count of each element in the current window.
- Initialize variables `i_chota`, `j`, and `i_bada` to keep track of the smallest valid and largest valid window(subarray).
2. **Sliding Window Technique**:
- Iterate over the array using two pointers `j` (the end of the window) and `i_chota` (the start of the window).
- At each step, increment the count of `nums[j]` in the HashMap and adjust the window boundaries to maintain exactly `k` distinct elements.
- If the count of distinct elements exceeds `k`, move the window's start pointer `i_chota` until the count is reduced to `k`.
- While moving `i_chota`, update `i_bada` to mark the new start of a valid subarray.
- Calculate the number of valid subarrays for each window configuration and update the result accordingly.
3. **Result**:
- The `result` variable accumulates the count of valid subarrays as the window slides through the array.
- Once the iteration is complete, return the final result.
This algorithm efficiently finds the count of subarrays with exactly `k` distinct elements using the sliding window technique and a HashMap to keep track of element counts within the window.
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
✨ Timelines✨
00:00 - Introduction
2:41 - Complete Intuition
11:52 - Detailed Dry Run
25:47 - Coding It Up
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024