Shortest Subarray with Sum at Least K | Already Studied Concept | Leetcode 862 | codestorywithMIK

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

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

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

    Good one sir,
    I solved 850+ questions on leetcode with 350+ days streak
    Still selected for 3.5LPA in TCS
    Feeling sad but i am still motivated and consistent that i will do Big someday
    Thanks for posting videos regularly

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

      congrats man. don't worry it's the start. you will get to your goal. have faith and keep grinding.

    • @guddukumar-v8z5l
      @guddukumar-v8z5l 10 ชั่วโมงที่ผ่านมา +2

      bhai konse college se ho.

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

      Hey can you can share your linkedin ID

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

      Congrats man celebrate every moment although your output is not equal to your effort but soon you will get to your goal

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

      contest rating kitna hai?

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

    Aaj monotonic stack/queue/deque ki need samaj ayi, thanks ❤

  • @Aditya-qx7nf
    @Aditya-qx7nf 3 ชั่วโมงที่ผ่านมา +2

    Thnx bro, loved the approach. Solved the medium level one without thinking but couldn't solve when negtive numbers were involved

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

    This was my approach for this question but failed to handle negative values : class Solution {
    public:
    int shortestSubarray(vector& nums, int k) {
    int len = INT_MAX;
    int l = 0;
    int r = 0;
    int n = nums.size();
    int sum = 0;
    while(r < n)
    {
    sum+=nums[r];
    while(sum >=k)
    {
    sum-=nums[l];
    len = min(len,r-l+1);
    l++;
    }
    r++;
    }
    if(len==INT_MAX)
    {
    return -1;
    }
    return len;
    }
    };

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

    0:45 yes sir and these are those people who are themselves going nowhere..

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

    amazing sir

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

    legend is here.
    i solved leetcode-209 as well. thanks to you

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

    thank you sir . awesome explanation.

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

    i eactly got stuck at the -ve part part the test was exactly the i was stuck 84 -37 32 40 95

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

    Hi Mazhar. Thanks for the video.
    @codestorywithMIK Just a *small suggestion/request* to add a unique hashtag or keyword to your video title. When stuck with any leetcode question, i often look for your explanation video but due to long channel name it is slightly cumbersome to find the video quickly. As of now, i personally use your git repo to find code snippets and related explanation videos.
    But if possible, you can include a simple and quick hashtag to your video or any content in general so that we can find your videos quickly. This might help in expanding the reach of channel too as more and more viewers will do organic searches for your hashtag.
    Rest, keep doing the great work!
    God bless you!

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

      Hi there,
      I appreciate your feedback. Thank you so much.
      Let me try to add #MIK from now onwards.
      That might help ❤️🙏

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

    Thanks a lot bhaiya ❤❤

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

    waiting for this video

  • @HarmanSingh-nw6ix
    @HarmanSingh-nw6ix 10 ชั่วโมงที่ผ่านมา +2

    good afternoon bhaiya ..

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

    Brother your explanation is awesome keep it up bro

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

    You explain great,
    But plz try to upload solutions of Leetcode contest at least of only EASY level

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

      Yes definitely. Actually i always travel during weekends to take break.
      I will try to upload them as well.
      Thank you for your kind words ❤️🙏

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

    Was waiting. Thanks a lot

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

    So sir ...You mean to say that suppose we wish to move ith pointer ...its possible that cumSum is larger or greater and we cannot obtain sum >= k and also if we get there maybe some smaller cumSum ahead of i and if we subtract it with that lesser cumSum we may get sum >= k ..SO we keep track of previous elements in increasing order so that we have min or less element to check with ( which gives higher chances of getting sum >= k ) and then for larger and again larger so as to see if we can shrink ...
    Am I Correct ?? IF NOT please correct sir 🙏🏻🙏🏻

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

      Are aise samjho.
      nums = {84, -37, 37, 40, 95}
      cumSum = {84, 47, 84, 124, 219}
      Let's say you are index j = 4, where cumSum is 219 >= k
      Now, you want to shrink the window.
      If you notice if you shrink and remove index 0 from left side i.e. 84
      Then subarray sum from index 1 to index 4 will be = cumSum[j] - cumSum[0] = 219 - 84 = 135
      But if you notice, even if you shrink the window further and remoce index 0 and index 1 from the window, you get the same answer= cumSum[j] - cumSum[2] = 219 - 84 = 135
      So there was no point of keeping the first 84 because there was a dip from 84 to 47 and we can get sum >= k further also which will be even shorter subarray.
      Hope this is clear

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

      @gui-codes yaa bro got that thanks

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

    Can't we keep count of negative no. We encounter in window if there is negative no. That means there is chance that sum would get increased hence we can shift the window towards left ..

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

    [80 -30 50] k = 100 so how we can remove 80? it needs to be added in subarray then only we will be able to make 100? I didn't get that part

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

      As soon as j reaches 50, the cumulativeSum[j] will become = 100
      And if you notice I have put a check that if cumulativeSum[j] >= k
      result = min(result, j+1)
      So result becomes = 3
      So we won’t miss that case also where we reach sum >= 100 here in this example
      That was a good example. Thanks for this Qn ❤️🙏

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

    Can we solved it using brute force?

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

      yes, but it will only give TLE. But you must start with Brute force (82/98 testcase passed)
      Java Brute force:
      class Solution {
      public int shortestSubarray(int[] nums, int k) {
      int len = 10000000; //any max number
      //check each subarray
      for(int i = 0; i < nums.length; i++){
      for(int j = i; j < nums.length; j++){
      int sum = 0;
      //window sum
      for(int s = i; s = k){
      len = Math.min(len, j - i + 1);
      }
      }
      }
      return len == 10000000 ? -1 : len;
      }
      }

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

    Solved the medium after watching your video but stuck on this problem
    😔😔

  • @yatheshtpoonia9225
    @yatheshtpoonia9225 34 นาทีที่ผ่านมา

    i was able to do the question you asked in the video but even after that aaj wala solution chamka nahi

  • @Sumit-wy4zp
    @Sumit-wy4zp 9 ชั่วโมงที่ผ่านมา

    sir please "sweep line " algorithm pe video banaiye..🥺🥺🥺🥺

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

    Can we solve this question by not using dequeue?

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

    Anyone need more clarity on why first 84 was removed from deque as soon as we saw a dip from 84 to 47. Here is more points to get more clarity.
    nums = {84, -37, 37, 40, 95}
    cumSum = {84, 47, 84, 124, 219}
    Let's say you are index j = 4, where cumSum is 219 >= k
    Now, you want to shrink the window.
    If you notice if you shrink and remove index 0 from left side i.e. 84
    Then subarray sum from index 1 to index 4 will be = cumSum[j] - cumSum[0] = 219 - 84 = 135
    But if you notice, even if you shrink the window further and remoce index 0 and index 1 from the window, you get the same answer= cumSum[j] - cumSum[2] = 219 - 84 = 135
    So there was no point of keeping the first 84 because there was a dip from 84 to 47 and we can get sum >= k further also which will be even shorter subarray.
    Hope this is clear.
    Thanks to MIK for improving my thinking skills.

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

    my solution without using dq.
    public int shortestSubarray(int[]nums,int k){
    int n=nums.length;
    long[]prefix =new long[n+1];
    for(int i=0;i

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

    Ist view as always ❤❤

  • @RishabhChatterjee-fg2gz
    @RishabhChatterjee-fg2gz 9 ชั่วโมงที่ผ่านมา +1

    Bhaiya mene bhi yehi example pe atak gya tha

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

    bhaiya please use dark theme in vs code😅😅

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

      Yes definitely. Actually i was travelling back home and hence forgot to switch to dark mode in rush. ❤️🙏

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

      @@codestorywithMIKthanks 😀 also please make video on infix to postfix and prefix conversion using stack

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

    First 🎉❤

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

    (1574. Shortest Subarray to be Removed to Make Array Sorted ) bhaiya please isa monotonic sa bna do ma 3 din sa try kar ra 🙂🙂

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

    First comment

  • @ex.comfy_error
    @ex.comfy_error 2 ชั่วโมงที่ผ่านมา

    sir black screen pe code kia kr ye pls ! anyways ,thanks for valuable effort

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

      Sure definitely. ❤️
      Next video in black screen

  • @DarkDragon-bz6qp
    @DarkDragon-bz6qp 8 ชั่วโมงที่ผ่านมา

    Why using Dequeue cant we use Stack here?As we can get increasing Sequence using Stack..What is the special advantage of using Double ended Queue ?it only allows insertion and deletion from both start and end

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

      The only benefit is that you can shrink the window from left side which is the requirement of this problem. You can pop from left side and push from right side.

    • @DarkDragon-bz6qp
      @DarkDragon-bz6qp 7 ชั่วโมงที่ผ่านมา +1

      @gui-codes Yeah got it thanks..Saw his previous video on power subarrays part 1

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

    bhaiya why 84 is irrelevant i didnt understand that part

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

      Are aise samjho.
      nums = {84, -37, 37, 40, 95}
      cumSum = {84, 47, 84, 124, 219}
      Let's say you are index j = 4, where cumSum is 219 >= k
      Now, you want to shrink the window.
      If you notice if you shrink and remove index 0 from left side i.e. 84
      Then subarray sum from index 1 to index 4 will be = cumSum[j] - cumSum[0] = 219 - 84 = 135
      But if you notice, even if you shrink the window further and remoce index 0 and index 1 from the window, you get the same answer= cumSum[j] - cumSum[2] = 219 - 84 = 135
      So there was no point of keeping the first 84 because there was a dip from 84 to 47 and we can get sum >= k further also which will be even shorter subarray.
      Hope this is clear

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

    majo me bhai ?

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

    sir leetcode ko dark mode kara karo plz

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

      Yes actually i was travelling back to home. In rush i missed to make dark theme.
      Apologies for inconvenience. Will take care from next video ❤️🙏

  • @AdarshShukla-nl1ij
    @AdarshShukla-nl1ij 9 ชั่วโมงที่ผ่านมา +1

    bhai java me code chahiye tha

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

      Please check the github link in the description . It has java code too ❤️

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

    why use deque why not just queue ?

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

      In que you won’t be able to pop from behind to maintain monotonic nature. Notice that in order to make it increasingly monotonic we will have to pop from behind.
      Also to shrink the window, we need to pop from front also. So deque provides both functionalites

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

    late ho gaya thoda

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

      Apologies for inconvenience.
      I am exploring Hyderabad this long weekend and hence the delay. ❤️🙏

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

      @@codestorywithMIK Dont even be sorry about it. I started solving daily challenges because of you posting the solutions later.