Continuous Subarrays | Detailed Approaches | Time Complexity | Leetcode 2762 | codestorywithMIK

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ธ.ค. 2024

ความคิดเห็น • 95

  • @rev_krakken70
    @rev_krakken70 วันที่ผ่านมา +40

    Solved it using your khandani sliding window template. Thanks MIK

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา +6

      Ye hui na baaaaat ❤️❤️❤️🔥🔥🔥

  • @bhanubediya4521
    @bhanubediya4521 23 ชั่วโมงที่ผ่านมา +31

    Hi MIK I have cracked a product based company 🥳 and interviewers were impressed by my problem solving skills ☺️ and even now whenever I get stuck on any problem I prefer watching your solution video always. Thank you so much❤

    • @girlwithwings4903
      @girlwithwings4903 21 ชั่วโมงที่ผ่านมา

      can you guide how ..

    • @girlwithwings4903
      @girlwithwings4903 21 ชั่วโมงที่ผ่านมา

      can you guide how ..

    • @divanshurawat8998
      @divanshurawat8998 17 ชั่วโมงที่ผ่านมา

      @@bhanubediya4521 on campus or off campus?

    • @gui-codes
      @gui-codes 16 ชั่วโมงที่ผ่านมา

      congrats man.

    • @codestorywithMIK
      @codestorywithMIK  16 ชั่วโมงที่ผ่านมา +2

      Many Many Congratulations 🎉🎉🙌🙌❤️❤️

  • @aws_handles
    @aws_handles วันที่ผ่านมา +17

    I quickly predicted that it’s a sliding window problem but I got stuck on how to find min and max in the window efficiently. I tried heap but got stuck with window shrink.
    Here to learn from you ❤❤❤

  • @vanshmodi14
    @vanshmodi14 12 ชั่วโมงที่ผ่านมา +3

    Better than watching 1hr webseries's episodes❤️

  • @ramanujtiwari5439
    @ramanujtiwari5439 15 ชั่วโมงที่ผ่านมา +4

    You are best tutor on youtube for DSA. I have watched many over the years, but no one explains building the approach better than you.

    • @codestorywithMIK
      @codestorywithMIK  15 ชั่วโมงที่ผ่านมา +2

      This means a lot ❤️🙏

  • @tapeshdua1545
    @tapeshdua1545 14 ชั่วโมงที่ผ่านมา +2

    I started watching your channel when there were only few subscribers around 6-7k. Now it's 76.3k, loved to see you growing. Keep up your good work! Your teaching skills are really invaluable.

  • @RohanN-s1r
    @RohanN-s1r 12 ชั่วโมงที่ผ่านมา +1

    Mik I got a job offer at an amazing American company. Major thanks to you

    • @thekindspill
      @thekindspill 5 ชั่วโมงที่ผ่านมา

      Congratulations bro . Do share your preparation strategy

  • @meetdobariya8777
    @meetdobariya8777 20 ชั่วโมงที่ผ่านมา +3

    kal leetcode 1438 dekha tha apka , aj vala same he to , khud se kar liya , THANKSSS

  • @Dnsndy
    @Dnsndy 12 ชั่วโมงที่ผ่านมา +1

    Thanks bhaiya ...your explanation is really exceptional, the way you explain and build the approach to solve question from scratch and explain why we need a particular data structure.
    Mahadev Matarani bless you

  • @KeshavKumar-jh2en
    @KeshavKumar-jh2en วันที่ผ่านมา +7

    thanku... .. u r literally legend for us❤solved on my own but need to see the vdo to learn from u

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา +2

      This means a lot to me ❤❤

    • @universalcosmologist3675
      @universalcosmologist3675 19 ชั่วโมงที่ผ่านมา

      @@codestorywithMIK pls make video on sweep line algo and on contest 4th questions of previous week weekly and biweekly contests plzzzzz

  • @Playvish
    @Playvish 12 ชั่วโมงที่ผ่านมา

    It's not optimal but coded it myself after watching till 14:02 ,feeling happy😊
    Thanks MIK bhaiya 🙏
    class Solution {
    public:
    bool valid(multiset & subarr){
    int mini = *(subarr.begin());
    int maxi = *(subarr.rbegin());
    return maxi-mini

  • @SaurabhSinghyadav-r4y
    @SaurabhSinghyadav-r4y 18 ชั่วโมงที่ผ่านมา +2

    I love 2nd approach ❤

  • @thebhrotherslakshya2005
    @thebhrotherslakshya2005 13 ชั่วโมงที่ผ่านมา +1

    Thanks bhaiya, Now i getting approaches to solve these type of questions by self

  • @Its_Shubham_Negi
    @Its_Shubham_Negi 15 ชั่วโมงที่ผ่านมา +5

    watched till 21:15, then coded myself.....legend mik 🔥

    • @codestorywithMIK
      @codestorywithMIK  15 ชั่วโมงที่ผ่านมา +1

      🙌🙌🙌

    • @Its_Shubham_Negi
      @Its_Shubham_Negi 15 ชั่วโมงที่ผ่านมา +2

      @codestorywithMIK thank u so much bhaiya
      U don't know your daily problems are changing many lives like me❤️❤️❤️

  • @faizanmohammed7687
    @faizanmohammed7687 4 ชั่วโมงที่ผ่านมา +1

    problems like these make me feel that I'll do it on my own next time. Wo sliding window ka problem mujhe ye hota ki while loop ke under sab code ka arrangement kaisa ho, like first while loop lagake shrink karna or first doing operations. Maybe not able to convey in words but less practice hai ismei.
    Lekin thanks great video!!!!

  • @CHANDANKUMAR-ck3is
    @CHANDANKUMAR-ck3is 13 ชั่วโมงที่ผ่านมา +1

    Great video , your style of writing code like story to code is awsome. i have started thinking in the same way . Lots of thans MIK.

  • @tejashsingh2060
    @tejashsingh2060 12 ชั่วโมงที่ผ่านมา +1

    nicely expained

  • @sauravchandra10
    @sauravchandra10 15 ชั่วโมงที่ผ่านมา +2

    Somehow when I cannot solve a problem on my own, I open your video, listen to your motivation, think about it, and then magically multiple possible approaches start coming to my mind. I can solve that problem on my own (not always optimized, but fast enough to get accepted) and this problem was one such example. Here is my version of the solution:
    class Solution:
    def continuousSubarrays(self, nums: List[int]) -> int:
    i, count = 0, 1
    maxList, minList = [(-nums[0], 0)], [(nums[0], 0)]
    for j in range(1, len(nums)):
    while maxList and abs(-maxList[0][0] - nums[j]) > 2:
    _, ind = heappop(maxList)
    i = ind+1
    while minList and abs(minList[0][0] - nums[j]) > 2:
    _, ind = heappop(minList)
    i = ind+1
    count += j-i+1
    heappush(maxList, (-nums[j], j))
    heappush(minList, ( nums[j], j))
    return count

    • @codestorywithMIK
      @codestorywithMIK  15 ชั่วโมงที่ผ่านมา

      So happy to know this ❤️❤️❤️

  • @DebopriyoBasu
    @DebopriyoBasu วันที่ผ่านมา +3

    Waiting for this video! Thanks!

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา +1

      Appreciate the wait, hope you find it useful! 🙏

  • @HarshSharma-hi9vc
    @HarshSharma-hi9vc 12 ชั่วโมงที่ผ่านมา +1

    resuming my dsa practice after diwali

  • @hana_0712
    @hana_0712 23 ชั่วโมงที่ผ่านมา +1

    Thank you sir
    I'm consistently following your videos and recent days I felt i was able to approach problems in more better way
    I'm in my 2nd year , I think by the start of 3rd year I would be in a good form in DSA.

  • @DineshKumar-gt7xm
    @DineshKumar-gt7xm 19 ชั่วโมงที่ผ่านมา +2

    Sir, please make explanation videos on Leetcode Contest questions. Especially on 3rd or 4th question.

  • @divanshurawat8998
    @divanshurawat8998 21 ชั่วโมงที่ผ่านมา +2

    We can also use multiset in place of ordered map as it can store dupplicate elements and store elements in sorted manner

  • @gui-codes
    @gui-codes 16 ชั่วโมงที่ผ่านมา

    Maza agaya. Each second is so valuable in your videos.
    Also, I understood the monotonic approach and coded on my own in O(n). Thanks to you for improving my DSA

  • @dibbodas4116
    @dibbodas4116 20 ชั่วโมงที่ผ่านมา +1

    your explanation is just wow🎉🎉 why do we need that data structure it is the main thing ❤

  • @gui-codes
    @gui-codes วันที่ผ่านมา +1

    was waiting. The Legend is here.

  • @jk-sm6qr
    @jk-sm6qr 18 ชั่วโมงที่ผ่านมา +2

    If we use normal queue data structure instead of Heap then we can acheive the time complexity of O(n). Use Monotonic Queue to maintain the max and min element in current window.

    • @jk-sm6qr
      @jk-sm6qr 18 ชั่วโมงที่ผ่านมา +1

      class Solution {
      public long continuousSubarrays(int[] nums) {
      long ans = 0;
      Deque min = new ArrayDeque();
      Deque max = new ArrayDeque();
      int i = 0;
      int n = nums.length;
      for (int j = 0; j < n; j++) {
      while (!min.isEmpty() && nums[min.peekLast()] > nums[j]) {
      min.pollLast();
      }
      min.addLast(j);
      while (!max.isEmpty() && nums[max.peekLast()] < nums[j]) {
      max.pollLast();
      }
      max.addLast(j);
      while (!min.isEmpty() && !max.isEmpty() && nums[max.peekFirst()] - nums[min.peekFirst()] > 2) {
      if (min.peekFirst() == i) {
      min.pollFirst();
      }
      if (max.peekFirst() == i) {
      max.pollFirst();
      }
      i++;
      }
      ans += j - i + 1;
      }
      return ans;
      }
      }
      Now it takes O(n) tc.

    • @codestorywithMIK
      @codestorywithMIK  16 ชั่วโมงที่ผ่านมา +2

      Definitely yes ❤️

  • @vaishnavigoswami5707
    @vaishnavigoswami5707 19 ชั่วโมงที่ผ่านมา +1

    truly legend

  • @gauravbanerjee2898
    @gauravbanerjee2898 12 ชั่วโมงที่ผ่านมา

    Thanks a lot bhaiya ❤❤

  • @utkarsh-k6p
    @utkarsh-k6p 22 ชั่วโมงที่ผ่านมา +2

    TLE -> 2134 / 2135
    If anyone is trying to solve it using min max concept .
    class Solution {
    public:
    long long continuousSubarrays(vector& nums) {
    int n = nums.size();
    int i = 0, j = 0;
    long long cnt = 0;
    int mini = nums[0], maxi = nums[0];
    while (j < n) {
    mini = min(mini, nums[j]);
    maxi = max(maxi, nums[j]);
    while (maxi - mini > 2) {
    i++;
    mini = *min_element(nums.begin() + i, nums.begin() + j + 1);
    maxi = *max_element(nums.begin() + i, nums.begin() + j + 1);
    }
    cnt += (j - i + 1);
    j++;
    }
    return cnt;
    }
    };

    • @batuklal
      @batuklal 18 ชั่วโมงที่ผ่านมา

      by using if statement u can save some time so it will pass:
      bool valid(int val1, int val2){
      return val2>=0&&val2=0&&val1

    • @utkarsh-k6p
      @utkarsh-k6p 18 ชั่วโมงที่ผ่านมา

      @batuklal yup It is Accepted now ... Can u explain why u write like mini==nums[i-1]

    • @batuklal
      @batuklal 17 ชั่วโมงที่ผ่านมา +1

      @@utkarsh-k6p bc we will only find minimum element when minimum element is equal to the element which we are moving ahead I++, so that minimum element is removed now we need to find new element from the subbarr y which is between I and j also if ith element is not the minimum element so we don't need to again find the minimum since it's inside our window so this will save lot of time

  • @anuragsekhri2315
    @anuragsekhri2315 22 ชั่วโมงที่ผ่านมา

    well explained.

  • @anandkishore1292
    @anandkishore1292 ชั่วโมงที่ผ่านมา +1

    yes mik bhyia i am ready for see the best side of me and a new more hardworking o person in 2025.

    • @codestorywithMIK
      @codestorywithMIK  ชั่วโมงที่ผ่านมา

      Let’s do it ❤️❤️❤️

  • @TanmayMankar-26
    @TanmayMankar-26 21 ชั่วโมงที่ผ่านมา +1

    Amazing

  • @kajiazadali7738
    @kajiazadali7738 วันที่ผ่านมา +5

    First Comment ❤❤❤❤❤❤❤❤❤❤

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา +3

      Appreciate it! 🙏❤️

    • @kajiazadali7738
      @kajiazadali7738 วันที่ผ่านมา +1

      @@codestorywithMIK Thank you Sir ❤❤❤❤❤❤❤❤❤❤

  • @SchedulrrVerificationGuide-m8q
    @SchedulrrVerificationGuide-m8q 14 ชั่วโมงที่ผ่านมา

    It would be great if you can make a video on O(n) approach of this

  • @utkarsh-k6p
    @utkarsh-k6p 23 ชั่วโมงที่ผ่านมา +1

    Bhaiya ispe video bnaa do jra .
    3116. kth smallest amount with single denomination combination

  • @hemantvishwakarma8751
    @hemantvishwakarma8751 22 ชั่วโมงที่ผ่านมา +1

    Thankyou

  • @rahulsihara8946
    @rahulsihara8946 13 ชั่วโมงที่ผ่านมา +1

    Used monotonic increasing and decreasing deque. That gives n time complexity.

    • @codestorywithMIK
      @codestorywithMIK  13 ชั่วโมงที่ผ่านมา

      Perfect 👌❤️

  • @xi1798
    @xi1798 20 ชั่วโมงที่ผ่านมา +2

    here is my solution with O(1) space
    int n = nums.size();
    int i=0,j=0;
    int mnidx = 0,mxidx = 0;
    int mnele = nums[0], mxele = nums[0];
    long long int ans = 0;
    while(j 2 || abs(nums[j]-mxele) > 2){
    mxele = nums[j], mnele = nums[j];
    int k = j-1;
    while(k>=i){
    mxele = max(mxele,nums[k]);
    mnele = min(mnele,nums[k]);
    if(abs(nums[k] - nums[j]) > 2) break;
    k--;
    }
    i = k+1;
    }
    ans += (j-i+1);
    mxele = max(mxele,nums[j]);
    mnele = min(mnele,nums[j]);
    j++;
    }
    return ans;

  • @rode_atharva
    @rode_atharva 17 ชั่วโมงที่ผ่านมา

    we can use multiset as well

  • @prateekbhaisora
    @prateekbhaisora 22 ชั่วโมงที่ผ่านมา +1

    Approach 3: Using Segment Tree (Java Code below)
    class Solution {
    private int[] segTreeMin;
    private int[] segTreeMax;
    public void buildSegTreeMin(int[] nums, int i, int l, int r) {
    if (l == r) {
    segTreeMin[i] = nums[l];
    return;
    }
    int lChild = (i

  • @themotivationfactory2274
    @themotivationfactory2274 20 ชั่วโมงที่ผ่านมา +1

    passed 1801/2135 test cases with this 😥😥:
    long long continuousSubarrays(vector& nums) {
    long long result = 0;
    int i=0;
    int j=0;
    while(j < nums.size()){
    if(abs(nums[i] - nums[j])

  • @abhishekkumar-fe8lw
    @abhishekkumar-fe8lw นาทีที่ผ่านมา

    Hi, can you tell me which tool are you using for explaining the solution?

  • @bishwashkumarsah171
    @bishwashkumarsah171 13 ชั่วโมงที่ผ่านมา

    bhaiya if possible can you make a video on
    2850. Minimum Moves to Spread Stones Over Grid
    recursion and backtracking approach.

  • @B-Billy
    @B-Billy 15 ชั่วโมงที่ผ่านมา +1

    Mik bhai can you please make a video on this question : 1588. Sum of All Odd Length Subarrays

    • @codestorywithMIK
      @codestorywithMIK  15 ชั่วโมงที่ผ่านมา

      Let me plan that soon ❤️

  • @rahuljeena
    @rahuljeena 23 ชั่วโมงที่ผ่านมา

    Can you plz tell the time complexity of inserting any key value pair in TreeMap vs HashMap in java?

  • @Jai_bharat_183
    @Jai_bharat_183 16 ชั่วโมงที่ผ่านมา +1

    v good soluti0n sir

  • @aryansrivastava6692
    @aryansrivastava6692 10 ชั่วโมงที่ผ่านมา

    Can it be solved using two monotone deque maintaining max and minimum elements in a window.???

  • @sauravchandra10
    @sauravchandra10 15 ชั่วโมงที่ผ่านมา

    Is there a way to implement the first approach using python while keeping the time complexity linear?

  • @AverageIndianDrivers
    @AverageIndianDrivers 21 ชั่วโมงที่ผ่านมา

    miku bhiyaa pls try solving lc 1712 a very standard questn of prefixsum+binary search pls do solve that

  • @HeetVichhivora
    @HeetVichhivora 15 ชั่วโมงที่ผ่านมา

    hello Mik bhaiya ,I hope this reaches you , please make a video of o(1) space complexity soln

    • @codestorywithMIK
      @codestorywithMIK  13 ชั่วโมงที่ผ่านมา +1

      Let me plan soon ❤️

  • @Nicky-qz6fr
    @Nicky-qz6fr 11 ชั่วโมงที่ผ่านมา

    Hii MIK,
    I have been solving questions for a while, But I am not able to identify testcase that would fail the program until I run it. This habit is causing problem in contest and OA as the test cases are hidden. How can i work on this issue?

  • @AahanRoy-l1i
    @AahanRoy-l1i 22 ชั่วโมงที่ผ่านมา +1

    maine simple sliding window se kiya to bs 10 test case me tle de diya😥😥

  • @tauheedahmed4073
    @tauheedahmed4073 วันที่ผ่านมา +2

    Tried brute force stuck at 1630/2134 😅

    • @codestorywithMIK
      @codestorywithMIK  วันที่ผ่านมา +1

      So glad to see you tried brute force first ❤️

  • @nihalsingh6233
    @nihalsingh6233 23 ชั่วโมงที่ผ่านมา

    How you got to know that size of map will never exceed 3 ??
    One more questions, some problems have 4 to 5 solutions !! So do we need to check all the 4 to 5 solutions ? If we are in beginner to intermediate stage ?

  • @SujalMahalaha
    @SujalMahalaha 13 ชั่วโมงที่ผ่านมา

    Bhaiya how we can think about DP in this question , like in leetcode some have solved this problem using dp , like here we have to check continuos subarray so how we can apply take or not take condition ? i just want to know that how we can think about dp in such problems?

  • @skypergaming5678
    @skypergaming5678 23 ชั่วโมงที่ผ่านมา

    Is there any data Structure in java like ordered set / ordered map because in my mind when you say that the elements arranged automatically in increasing order then it is a concept of heap but it is not heap. Can you clarify ? Please...
    Which Data Structure we use in Java

    • @DebopriyoBasu
      @DebopriyoBasu 22 ชั่วโมงที่ผ่านมา +1

      You can use TreeMap.
      TreeMap freq = new TreeMap();
      For Tree map you can use this condition:
      while (freq.lastEntry().getKey() - freq.firstEntry().getKey() > 2) {...}

  • @B-Billy
    @B-Billy 23 ชั่วโมงที่ผ่านมา +1

    Mazhar bhai, At 6:50, why did not we include {2,2}? This is also a valid subarray.
    Aksing this because at 3:47 you selected {5,2} but did not include because it not a valid subarray as per the contrains.

    • @vaibhavshukla6911
      @vaibhavshukla6911 23 ชั่วโมงที่ผ่านมา

      Bcz subarray is contiguous

    • @B-Billy
      @B-Billy 23 ชั่วโมงที่ผ่านมา

      @@vaibhavshukla6911 Argh!! Now I get it.
      Actually Mazhar did not select {5,2}, but {5,4,2}. And this selected subarray {5,4,2} is invalid because
      of {'5', 4, '2'}.
      Thanks bhai.

  • @nehakushwah8055
    @nehakushwah8055 19 ชั่วโมงที่ผ่านมา

    you said 1+2+3 = 7 at 7:22

    • @codestorywithMIK
      @codestorywithMIK  18 ชั่วโมงที่ผ่านมา +1

      Apologies for silly mistake. 😇

  • @omkarsawant9267
    @omkarsawant9267 ชั่วโมงที่ผ่านมา

    Deques Are Better Here than Heap
    Optimal Time Complexity: Deques provide 𝑂(1)
    O(1) push and pop operations, ensuring the sliding window runs in linear time. Heaps, with 𝑂(log𝑛)
    O(logn) operations, introduce unnecessary overhead.
    Simplicity: Deques are straightforward to manage, while heaps require additional logic to handle outdated indices (e.g., lazy deletion or a hash map).

  • @Jai_bharat_183
    @Jai_bharat_183 16 ชั่วโมงที่ผ่านมา

    but bhaiya frequency ka koiuse nhi hai