Continuous Subarrays | Detailed Approaches | Time Complexity | Leetcode 2762 | codestorywithMIK
Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
This is the 33rd Video of our Playlist "Sliding Window : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a classic but a little tricky Sliding Window Problem : Continuous Subarrays | Detailed Approaches | Time Complexity | Leetcode 2762 | codestorywithMIK
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 : Continuous Subarrays | Detailed Approaches | Time Complexity | Leetcode 2762 | codestorywithMIK
Company Tags : will update later
My solutions on Github(C++ & JAVA) - https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Sliding Window/Continuous Subarrays.cpp
Leetcode Link : https://leetcode.com/problems/continuous-subarrays
My DP Concepts Playlist : • Roadmap for DP | How to Start DP ? | ...
My Graph Concepts Playlist : • Graph Concepts & Qns - 1 : Graph will...
My Recursion Concepts Playlist : • Introduction | Recursion Concepts And...
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 : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Approach 1: Using Ordered Map (map)
Idea:
Use a map to maintain the frequency of elements in the current sliding window.
The map is naturally ordered, so the smallest and largest elements can be accessed in O(1) using begin() and rbegin().
Process:
Expand the window by incrementing j and updating the map with the frequency of nums[j].
Check the difference between the largest and smallest elements (rbegin() first and begin() first).
If the difference exceeds 2, shrink the window by incrementing i and updating the map.
Add the number of valid subarrays ending at j to the result.
Approach 2: Using Min-Heap and Max-Heap
Idea:
Use two heaps (priority_queue) to track the smallest and largest elements in the current sliding window.
Min-heap tracks the smallest element, and max-heap tracks the largest element.
Process:
Expand the window by incrementing j and adding nums[j] to both heaps.
Check the difference between the largest (maxHeap.top()) and smallest (minHeap.top()) elements.
If the difference exceeds 2, shrink the window by incrementing i.
Remove elements from the heaps that are no longer part of the window (indices less than i).
Add the number of valid subarrays ending at j to the result.
✨ Timelines✨
00:00 - Introduction
0:29 - Motivation
1:00 - Problem Explanation
4:54 - Thought Process Approach-1
28:43 - Coding Approach-1
33:01 - Time Complexity of Approach-1
37:16 - Thought Process Approach-2
47:06 - Coding Approach-2
#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 #coding #programming #100daysofcode #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #javascript #datascience #leetcode #leetcodesolutions #leetcodedailychallenge #codinginterview #interviewprep #technicalinterview #interviewtips #interviewquestions #codingchallenges #interviewready #dsa #hindi #india #hindicoding #hindiprogramming #hindiexplanation #hindidevelopers #hinditech #hindilearning #helpajobseeker #jobseekers #jobsearchtips #careergoals #careerdevelopment #jobhunt #jobinterview #github #designthinking #learningtogether #growthmindset #digitalcontent #techcontent #socialmediagrowth #contentcreation #instagramreels #videomarketing #codestorywithmik #codestorywithmick #codestorywithmikc #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik