Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | Leetcode 1438 |Detailed

Sdílet
Vložit
  • čas přidán 21. 06. 2024
  • Whatsapp Community Link : www.whatsapp.com/channel/0029...
    This is the 24th Video of our Playlist "Sliding Window : Popular Interview Problems" by codestorywithMIK
    In this video we will try to solve a good and standard Sliding Window Problem : Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | 2 Approaches | Dry Runs | Thought Process | Leetcode 1438 | 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 : Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | 2 Approaches | Dry Runs | Thought Process | Leetcode 1438 | codestorywithMIK
    Company Tags : UBER
    My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/longest...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My Recursion Concepts Playlist : • Introduction | Recursi...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Subscribe to my channel : / @codestorywithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    Summary :
    Approach-1: Using Sliding Window + Heap
    Algorithm: This approach employs two heaps (priority queues) to maintain the maximum and minimum values within the current sliding window.
    Max-Heap: Keeps track of the maximum values.
    Min-Heap: Keeps track of the minimum values.
    Both heaps store pairs of values and their indices.
    As the window slides, the heaps are updated to ensure that the difference between the maximum and minimum values within the window does not exceed the specified limit.
    If the difference exceeds the limit, the start of the window (i) is adjusted, and elements are popped from the heaps if they fall outside the new window.
    The length of the longest valid subarray is continuously updated.
    Time Complexity: O(n log n) due to heap operations.
    Space Complexity: O(n) to store elements in the heaps.
    Approach-2: Using Sliding Window + Multiset
    Algorithm: This approach uses a multiset to maintain all elements in the current sliding window, allowing efficient access to the minimum and maximum values.
    As new elements are added to the window, they are inserted into the multiset.
    If the difference between the maximum and minimum values exceeds the limit, the start of the window (i) is incremented, and the corresponding element is removed from the multiset.
    The length of the longest valid subarray is updated in each iteration.
    Time Complexity: O(n log n) due to multiset operations (insertions and deletions).
    Space Complexity: O(n) to store elements in the multiset.
    Comparison
    Both approaches use a sliding window technique combined with a data structure that supports efficient retrieval of the minimum and maximum values within the window.
    Approach-1 (Using Heaps):
    More complex due to maintaining two heaps.
    Slightly more operations required to keep the heaps balanced and valid.
    Approach-2 (Using Multiset):
    Simpler to implement.
    Direct access to the minimum and maximum values using rbegin() and begin().
    Both approaches have the same time and space complexity but differ in the data structures used and the specific implementation details.
    ✨ Timelines✨
    00:00 - Introduction
    #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

Komentáře • 80

  • @Polly10189
    @Polly10189 Před měsícem +31

    You have a God gifted talent of intelligence and explaining it so simply. Than you so much🎉

  • @ramandeepsingh8464
    @ramandeepsingh8464 Před měsícem +22

    Please don't stop posting video and if you have time then please do contest discussion also

  • @kaustubhchaudhari9838
    @kaustubhchaudhari9838 Před měsícem +7

    God Gifted Mic Bhai, Thank you for Explaining so easily

  • @user13443fg
    @user13443fg Před měsícem +15

    Nice you stopped writing topic name in video thumbnail. It is necessary to understand problem first and build Intuition. i hope you will not show topics in video thumbnail in your future videos as well :)

  • @anjanasahay2172
    @anjanasahay2172 Před měsícem +5

    Yes Please Contest problems too...the hard and medium ones❤❤

  • @ShardulPrabhu
    @ShardulPrabhu Před měsícem +1

    Thanks sir ❤ only question explain kr k pause krne bataya uske liye mere 😢 you care for us 😢😢 thank you so much

  • @StackUnderFlow8055
    @StackUnderFlow8055 Před měsícem +8

    today i did the first question with a multiset , amazing explanation ❤❤

  • @pavittarkumarazad3259
    @pavittarkumarazad3259 Před měsícem +4

    Thanks, approach 2 was really amazing. I was able to solve it on my own with the 1st approach but what was more amazing was that you showed us why we need a TreeMap data structure while building the intuition.

  • @saminaabbas9722
    @saminaabbas9722 Před měsícem +1

    Thank you MIK! You're the best

  • @ugcwithaddi
    @ugcwithaddi Před měsícem +3

    Complete legend. Detailed each step

  • @floatingpoint7629
    @floatingpoint7629 Před měsícem +1

    thanks for the multiple dry run

  • @user-ym1nv1pw8i
    @user-ym1nv1pw8i Před měsícem +1

    Thanks a lot sir!

  • @study-yd6es
    @study-yd6es Před měsícem

    Amazing Explainations!!😍

  • @VedBrahmbhattBEE
    @VedBrahmbhattBEE Před měsícem

    Awesome as always.

  • @EB-ot8uu
    @EB-ot8uu Před 27 dny

    legit. thanks a lot legend

  • @tarunsingh2480
    @tarunsingh2480 Před měsícem +1

    great explanation man ...

  • @gauravbanerjee2898
    @gauravbanerjee2898 Před měsícem +1

    Thanks a lot bhaiya ❤❤ Bohot hi pyara explanation tha 🔥

  • @cricketsureshlucky
    @cricketsureshlucky Před měsícem

    Thank you so much

  • @AnjuGupta-sz1uk
    @AnjuGupta-sz1uk Před měsícem +1

    Amazing Expalnation 😊

  • @Vinamrasharma0523
    @Vinamrasharma0523 Před měsícem +1

    Waiting for this ❤

  • @priyanshkumariitd
    @priyanshkumariitd Před měsícem

    Great Explanation Bhaiya!!! Thank you...

  • @chitranshjain9714
    @chitranshjain9714 Před měsícem +6

    Please resume dp concept series

  • @23cash86
    @23cash86 Před měsícem +1

    Great video as always

  • @gui-codes
    @gui-codes Před měsícem

    I felt this was hard but man you explain so well.

  • @rahulnegi4027
    @rahulnegi4027 Před měsícem

    Bhaiya pleas leetcode ke contest ke question ke solution bhi explain krdo please apki explanation boht badiya lagti h please.....
    waiting for the series where you will do contest problems

  • @chitranshjain9714
    @chitranshjain9714 Před měsícem

    amazing explanation

  • @venkatarohitpotnuru38
    @venkatarohitpotnuru38 Před měsícem +1

    Bhaiya weekend aagya..partion dp concept videos

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Před měsícem

    Amazing explanation man

  • @manishdubey4772
    @manishdubey4772 Před měsícem +1

    @codestorywithMIK Hi sir, you discuss the 2nd approach which is using the TreeMap but there also arises one problem i.e if we have all unique elements then we have to traverse through all the elements of the map as I think there is no any way to get the smallest and largest element directly in O(1) time complexity. Can you please discuss it?
    At last, I really like your explanation of the question problem like a story, I want to thank you for your great videos

  • @animestuff144
    @animestuff144 Před měsícem

    Awesome explanation sir

  • @anshror2583
    @anshror2583 Před měsícem +2

    Bhai aap please contest ki 3 and 4 problem discuss kara laro please bhai request h

  • @ashutoshpandey1639
    @ashutoshpandey1639 Před měsícem +1

    I have solved this prolem using multiset, I think multiset wala approach is more intuitive than using priority queue

  • @ArpitDhimaan
    @ArpitDhimaan Před měsícem +2

    bro pls add time stamp in future videos. It , saves lot of time.

  • @arjunprasad1071
    @arjunprasad1071 Před měsícem

    Great explaination!
    if its possible please make an explainer on the O(N) approach ie deque approach🙏🙏

  • @ashish3487
    @ashish3487 Před měsícem

    sir make some more questions video on segment tree

  • @CodeMode9313
    @CodeMode9313 Před měsícem +1

    Little correction !!!! TIme stamp 9:19 , a lil minute error , diff b/w max and min is 7 not 6

  • @ramandeepsingh8464
    @ramandeepsingh8464 Před měsícem

  • @pokeindia5361
    @pokeindia5361 Před měsícem

    Bhaiya aapne bola tha weekend pe *longest valid parantheses* ka video daaloge... Please bhaiya ye important q h interview k liye

  • @abhinavnarang4369
    @abhinavnarang4369 Před měsícem

    bhaiya segmenr tree ki playlist continue krdo,interview h

  • @aizad786iqbal
    @aizad786iqbal Před měsícem

    one doubt, pop should be O(1) right, as we just delete the top element....
    the push is log(n) time
    correct ?
    please confirm...

  • @prakharTiwari-c8n
    @prakharTiwari-c8n Před měsícem

    @codestorywithMIK , Thanks for your so detailed video but how TreeMap store duplicate value ?

    • @anishbishnoi29xD
      @anishbishnoi29xD Před měsícem

      i think TreeSet does not store duplicate value but TreeMap can store

  • @manpreetsaluja3691
    @manpreetsaluja3691 Před měsícem

    I do it with map and sliding window concept

  • @aizad786iqbal
    @aizad786iqbal Před měsícem

    i tried using in tree set in java..but it doesn't store duplicate elements..

  • @ReshuSharma-jc7yu
    @ReshuSharma-jc7yu Před měsícem

    Sir mereko kaise pata chalega ki isme heap use hoga question main ki nhi

  • @harmansingh722
    @harmansingh722 Před měsícem

    Which notepad do you use?

  • @amar4429
    @amar4429 Před měsícem

    [1,5,6,7,8,10,6,5,6]
    limit:- 4
    Answer = 5
    can you explain this example
    please👃

  • @ManojKrVerma-vw4dx
    @ManojKrVerma-vw4dx Před měsícem +1

    I could solve this by sliding window and multiset.

  • @MayankRathore-qi7qv
    @MayankRathore-qi7qv Před měsícem

    sir mujhe yeh max heap ur min heap wala question smghj nhi aaya

  • @surekhasonawane4045
    @surekhasonawane4045 Před měsícem

    Solved using Multiset as we can get min/max in O(1) . and deletion in O(1) whenever window's absolute difference exceed limit

    • @gui-codes
      @gui-codes Před měsícem

      bhai multiset jaise STLs ko kaha se parha ?
      Can you share any good resource.

    • @jitesh_khurana
      @jitesh_khurana Před měsícem

      @@gui-codes gfg se padhle bhai

    • @jitesh_khurana
      @jitesh_khurana Před měsícem

      int longestSubarray(vector& nums, int limit) {
      int ans = 1;
      int n = nums.size();
      int i = 0;
      int j = 0;
      multiset st;
      while(j

  • @akashsaurabh9196
    @akashsaurabh9196 Před měsícem +1

    Why st.erase(st. Find(nums[i])) why not st. Erase(nums[i])??

    • @codestorywithMIK
      @codestorywithMIK  Před měsícem +1

      There are 3 variants of erase in multiset.
      1) void erase (iterator position);
      2) size_type erase (const value_type& val);
      3) void erase (iterator first, iterator last);
      If you use 1st one, it will only delete the single element pointed by the iterator position.
      If you use 2nd one, it will delete all occurrences of that element from the multiset. We don’t want that here because we can have duplicate elements.

  • @cricketsureshlucky
    @cricketsureshlucky Před měsícem +4

    Sir my kind request please upload a video on today's contest 3rd question. It is quite difficult solving dp.

    • @ramandeepsingh8464
      @ramandeepsingh8464 Před měsícem +1

      Exactly 💯

    • @RitikRanjan-fn9me
      @RitikRanjan-fn9me Před měsícem +2

      try to think something related to consecutive negative numbers and apply pick not pick conditions accordingly

    • @cp65143
      @cp65143 Před měsícem

      +1

  • @deepanshuverma6237
    @deepanshuverma6237 Před měsícem

    rep++

  • @Innovision2023
    @Innovision2023 Před měsícem +1

    Bhaiya tumara linkedin id thetho i will mention u in my leetcode acheivements

    • @codestorywithMIK
      @codestorywithMIK  Před měsícem

      Hey,
      That’s sweet of you ❤️
      www.linkedin.com/in/mazhar-imam-khan-95a34ab3?

  • @user-je1nh6qq7x
    @user-je1nh6qq7x Před měsícem +3

    this question must be marked hard

    • @gui-codes
      @gui-codes Před měsícem

      sahi me yaar.

    • @naive-fleek7420
      @naive-fleek7420 Před měsícem

      @@gui-codes nahi yarr multiset use krte ho toh bhot easy hai , easy medium hoga

  • @empvaibhav9799
    @empvaibhav9799 Před měsícem

    Solve "Leetcode : 239. Sliding Window Maximum" before attempting this.

  • @RahulJohn-sv8gz
    @RahulJohn-sv8gz Před měsícem

    Can you make your video in english

  • @anish.naruto
    @anish.naruto Před měsícem

    Sir, one thing i want to point out that you always take elements sorted in heaps.. But that's not always true in heaps, only the top element can be surely claimed as min or max... And heap structure changes on addition or deletion of elements to maintain that top value condition... So it's not good to talk like this that the max element will be in bottommost position in minheap or something like that. Though, i understand you're just taking that way to explain it better way but people can have misconception about heaps by this.
    Correct me if I'm wrong.

  • @user-lg1td4lg3x
    @user-lg1td4lg3x Před měsícem

    💥💥💥💥💥explain most optimized solution from leetcode solution section💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥

  • @jitesh_khurana
    @jitesh_khurana Před měsícem +5

    Preety Good Easy Solution with multiset and sliding window
    int longestSubarray(vector& nums, int limit) {
    int ans = 1;
    int n = nums.size();
    int i = 0;
    int j = 0;
    multiset st;
    while(j

  • @varunkedia6729
    @varunkedia6729 Před měsícem

    Question says - difference b/w any two elements
    Then why are we talking diff of largest and smallest?

    • @gui-codes
      @gui-codes Před měsícem

      See 5:03 in the video bro.
      The question say difference b/w any two elements MUST not exceed limit.
      So, for any subarray, instead of checking diference b/w every possible 2 elements pair (if they are crossing limit or not), you can simply check if the difference between highest and lowest number in that subarray is

  • @ks-xh4fq
    @ks-xh4fq Před měsícem

    50 mins for 1 qs explanation ??

  • @naive-fleek7420
    @naive-fleek7420 Před měsícem +1

    o(n) wala approach nahi bataye aap?

  • @user-lg1td4lg3x
    @user-lg1td4lg3x Před měsícem

    please share your slides

  • @manibhushankumarsingh5196
    @manibhushankumarsingh5196 Před měsícem +3

    00:07 Find the longest continuous subarray with an absolute difference less than or equal to a given limit
    02:12 Finding the longest continuous subarray with absolute difference less than or equal to limit.
    06:23 To validate a sub-array, find the difference between its maximum and minimum elements.
    08:27 Identifying valid subarrays based on difference limit.
    12:38 Using a heap can efficiently track the minimum element within the window.
    14:43 Implementing window sliding technique for efficient computation
    18:33 Analyzing subarrays based on element limits
    20:47 Algorithm for finding longest continuous subarray with absolute difference less than or equal to limit
    24:13 Dry runs help in gaining clarity for solving problems.
    25:59 Understanding the shifting of elements in the max and min heaps during traversal.
    29:41 Finding valid subarrays with absolute difference less than limit.
    31:40 Iterating through the array and checking limits for valid subarrays
    35:40 Algorithm for finding longest continuous subarray with absolute difference less than or equal to limit
    38:07 Discussed time complexity and approach for a sliding window problem
    41:58 Efficient data structure for finding maximum and minimum elements with a given limit
    43:50 Explanation of using Multi Set in C++ for efficient operations
    47:20 Explanation of using max heap and multi set for inserting and removing elements.

  • @ago7506
    @ago7506 Před měsícem +1

    day-23 of saying lord🫡

  • @Engineering.Wallah
    @Engineering.Wallah Před měsícem

    TLE
    //int n=nums.size(),ans=0;
    //for(int i=0;i