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

Поділитися
Вставка
  • Опубліковано 27 сер 2024
  • Whatsapp Community Link : www.whatsapp.c...
    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/MAZ...
    Leetcode Link : leetcode.com/p...
    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/MAZ...
    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

КОМЕНТАРІ • 83

  • @codestorywithMIK
    @codestorywithMIK  26 днів тому +1

    Correction - Video - 24

  • @Polly10189
    @Polly10189 2 місяці тому +31

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

  • @ramandeepsingh8464
    @ramandeepsingh8464 2 місяці тому +22

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

  • @kaustubhchaudhari9838
    @kaustubhchaudhari9838 2 місяці тому +7

    God Gifted Mic Bhai, Thank you for Explaining so easily

  • @user13443fg
    @user13443fg 2 місяці тому +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 2 місяці тому +5

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

  • @pavittarkumarazad3259
    @pavittarkumarazad3259 2 місяці тому +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.

  • @StackUnderFlow8055
    @StackUnderFlow8055 2 місяці тому +8

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

  • @manibhushankumarsingh5196
    @manibhushankumarsingh5196 2 місяці тому +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.

  • @ugcwithaddi
    @ugcwithaddi 2 місяці тому +3

    Complete legend. Detailed each step

  • @saminaabbas9722
    @saminaabbas9722 2 місяці тому +1

    Thank you MIK! You're the best

  • @floatingpoint7629
    @floatingpoint7629 2 місяці тому +1

    thanks for the multiple dry run

  • @ShardulPrabhu
    @ShardulPrabhu 2 місяці тому +1

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

  • @user-ym1nv1pw8i
    @user-ym1nv1pw8i 2 місяці тому +1

    Thanks a lot sir!

  • @ArpitDhimaan
    @ArpitDhimaan 2 місяці тому +2

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

  • @chitranshjain9714
    @chitranshjain9714 2 місяці тому +6

    Please resume dp concept series

  • @rahulnegi4027
    @rahulnegi4027 2 місяці тому

    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

  • @gui-codes
    @gui-codes 2 місяці тому

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

  • @ashutoshpandey1639
    @ashutoshpandey1639 2 місяці тому +1

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

  • @gauravbanerjee2898
    @gauravbanerjee2898 2 місяці тому +1

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

  • @venkatarohitpotnuru38
    @venkatarohitpotnuru38 2 місяці тому +1

    Bhaiya weekend aagya..partion dp concept videos

  • @CodeMode9313
    @CodeMode9313 2 місяці тому +1

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

  • @manishdubey4772
    @manishdubey4772 2 місяці тому +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

  • @EB-ot8uu
    @EB-ot8uu Місяць тому

    legit. thanks a lot legend

  • @23cash86
    @23cash86 2 місяці тому +1

    Great video as always

  • @AnjuGupta-sz1uk
    @AnjuGupta-sz1uk 2 місяці тому +1

    Amazing Expalnation 😊

  • @anshror2583
    @anshror2583 2 місяці тому +2

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

  • @arjunprasad1071
    @arjunprasad1071 2 місяці тому

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

  • @priyanshkumar_iitd
    @priyanshkumar_iitd 2 місяці тому

    Great Explanation Bhaiya!!! Thank you...

  • @tarunsingh2480
    @tarunsingh2480 2 місяці тому +1

    great explanation man ...

  • @animestuff144
    @animestuff144 2 місяці тому

    Awesome explanation sir

  • @cricketsureshlucky
    @cricketsureshlucky 2 місяці тому +4

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

    • @ramandeepsingh8464
      @ramandeepsingh8464 2 місяці тому +1

      Exactly 💯

    • @RitikRanjan-fn9me
      @RitikRanjan-fn9me 2 місяці тому +2

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

    • @cp65143
      @cp65143 2 місяці тому

      +1

  • @empvaibhav9799
    @empvaibhav9799 2 місяці тому

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

  • @Vinamrasharma0523
    @Vinamrasharma0523 2 місяці тому +1

    Waiting for this ❤

  • @study-yd6es
    @study-yd6es 2 місяці тому

    Amazing Explainations!!😍

  • @user-ub2is4rs4x
    @user-ub2is4rs4x 2 місяці тому

    Amazing explanation man

  • @surekhasonawane4045
    @surekhasonawane4045 2 місяці тому

    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 2 місяці тому

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

    • @jitesh_khurana
      @jitesh_khurana 2 місяці тому

      @@gui-codes gfg se padhle bhai

    • @jitesh_khurana
      @jitesh_khurana 2 місяці тому

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

  • @VedBrahmbhattBEE
    @VedBrahmbhattBEE 2 місяці тому

    Awesome as always.

  • @jitesh_khurana
    @jitesh_khurana 2 місяці тому +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

  • @chitranshjain9714
    @chitranshjain9714 2 місяці тому

    amazing explanation

  • @SanjeevKumar-xv5lw
    @SanjeevKumar-xv5lw Місяць тому

    Why didn't we use two deques for maintaining min and Max of the window avoiding that extra log n term which comes with heap and multiset?

  • @aizad786iqbal
    @aizad786iqbal 2 місяці тому

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

  • @cricketsureshlucky
    @cricketsureshlucky 2 місяці тому

    Thank you so much

  • @amar4429
    @amar4429 2 місяці тому

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

  • @ashish3487
    @ashish3487 2 місяці тому

    sir make some more questions video on segment tree

  • @harshsingh7713
    @harshsingh7713 15 днів тому

    Guru ji Longest Nice subarray pe solution bna do please

  • @user-je1nh6qq7x
    @user-je1nh6qq7x 2 місяці тому +3

    this question must be marked hard

    • @gui-codes
      @gui-codes 2 місяці тому

      sahi me yaar.

    • @naive-fleek7420
      @naive-fleek7420 2 місяці тому

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

  • @abhinavnarang4369
    @abhinavnarang4369 2 місяці тому

    bhaiya segmenr tree ki playlist continue krdo,interview h

  • @prakharTiwari-c8n
    @prakharTiwari-c8n 2 місяці тому

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

    • @anishbishnoi29xD
      @anishbishnoi29xD 2 місяці тому

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

  • @pokeindia5361
    @pokeindia5361 2 місяці тому

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

  • @aizad786iqbal
    @aizad786iqbal 2 місяці тому

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

  • @anish.naruto
    @anish.naruto 2 місяці тому

    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.

  • @akashsaurabh9196
    @akashsaurabh9196 2 місяці тому +1

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

    • @codestorywithMIK
      @codestorywithMIK  2 місяці тому +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.

  • @manpreetsaluja3691
    @manpreetsaluja3691 2 місяці тому

    I do it with map and sliding window concept

  • @ReshuSharma-jc7yu
    @ReshuSharma-jc7yu 2 місяці тому

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

  • @ManojKrVerma-vw4dx
    @ManojKrVerma-vw4dx 2 місяці тому +1

    I could solve this by sliding window and multiset.

  • @harmansingh722
    @harmansingh722 2 місяці тому

    Which notepad do you use?

  • @MayankRathore-qi7qv
    @MayankRathore-qi7qv 2 місяці тому

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

  • @varunkedia6729
    @varunkedia6729 2 місяці тому

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

    • @gui-codes
      @gui-codes 2 місяці тому

      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

  • @Innovision2023
    @Innovision2023 2 місяці тому +1

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

    • @codestorywithMIK
      @codestorywithMIK  2 місяці тому

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

  • @deepanshuverma6237
    @deepanshuverma6237 2 місяці тому

    rep++

  • @naive-fleek7420
    @naive-fleek7420 2 місяці тому +1

    o(n) wala approach nahi bataye aap?

  • @ramandeepsingh8464
    @ramandeepsingh8464 2 місяці тому

  • @user-lg1td4lg3x
    @user-lg1td4lg3x 2 місяці тому

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

  • @RahulJohn-sv8gz
    @RahulJohn-sv8gz 2 місяці тому

    Can you make your video in english

  • @ks-xh4fq
    @ks-xh4fq 2 місяці тому

    50 mins for 1 qs explanation ??

  • @user-lg1td4lg3x
    @user-lg1td4lg3x 2 місяці тому

    please share your slides

  • @Engineering.Wallah
    @Engineering.Wallah 2 місяці тому

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

  • @ago7506
    @ago7506 2 місяці тому +1

    day-23 of saying lord🫡