Count Subarrays With Fixed Bounds | Made Simple | Microsoft | Leetcode - 2444 | codestorywithMIK

Поделиться
HTML-код
  • Опубликовано: 16 сен 2024
  • This is the 7th Video of our Sliding Window Playlist.
    In this video we will try to solve another very very good problem "Find the Index of the First Occurrence in a String". (Leetcode-2444)
    Problem Name : Count Subarrays With Fixed Bounds
    Company Tags : Microsoft
    My solutions on Github : github.com/MAZ...
    Leetcode Link : leetcode.com/p...
    My GitHub Repo for interview preparation : github.com/MAZ...
    Subscribe to my channel : / @codestorywithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
    #interviewpreparation #interview_ds_algo #hinglish

Комментарии • 123

  • @alfansea
    @alfansea 20 дней назад +2

    asked by meesho in today's OA

    • @mohdalizilani9896
      @mohdalizilani9896 19 дней назад

      i have meesho OT today... what other questions they asked could you tell???

  • @sonalipatro2087
    @sonalipatro2087 Год назад +27

    I would say THIS IS CRYSTAL CLEAR EXPLANATION !! 👌👌👌
    LOVED the CULPRIT INDEX CONCEPT. Enjoyed AND learnt in one go. CREDIT : YOU
    After understanding, I feel like I won't forget the solution .
    Thank you so much for your contributionto the coding society.🙌🙌

  • @gauravmishra1341
    @gauravmishra1341 5 месяцев назад +14

    It was your day 2 of the streak,and now you are near at around 400 days on leetcode,great bro

    • @Rajdweep
      @Rajdweep 5 месяцев назад

      just saw day2 and thought wtf, then saw the date od upload 😂 anyways great explanation man at first it looked like sliding window to me

  • @anurag1545
    @anurag1545 Год назад +13

    Today I had seen many videos solution of this question but none of these are able to solve this problem in your way..
    Your way of solving problem is totally different from others and I really love it and appreciate your work....❤

  • @JJ-tp2dd
    @JJ-tp2dd Год назад +4

    Thanks bhai, what a lovely explanation! Below is the Java code:
    class Solution {
    public long countSubarrays(int[] nums, int minK, int maxK) {
    long ans = 0;
    int minKPosition = -1;
    int maxKPosition = -1;
    int culpritIndex = -1;

    for(int i=0; i maxK) {
    culpritIndex = i;
    }
    if(nums[i] == minK) {
    minKPosition = i;
    }
    if(nums[i] == maxK) {
    maxKPosition = i;
    }

    int smaller = Math.min(minKPosition, maxKPosition);
    long temp = smaller - culpritIndex;

    ans+= (temp

  • @mohdsaqib5306
    @mohdsaqib5306 5 дней назад +1

    this channel will boom in coming days out of nowhere

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Год назад +8

    Very beautiful explanation. This channel going top

  • @souravjoshi2293
    @souravjoshi2293 Год назад +3

    Like Anurag also mentioned, I watched many videos on RUclips on this Qn.
    But the explanation of this guy is way tooo good man. You are just too different bro. I don't know if you realise this , but you are really gifted. Keep it up.

  • @sidharthdhiman4522
    @sidharthdhiman4522 Год назад +4

    sir itna hard question ko itna easy banadiya apne , god level explanaiton

  • @elakstein
    @elakstein Год назад +1

    I come here daily after solving question to see how you solved and sometimes to grasp the idea. Good initiative, carry on.

  • @RISHURAJ-jd5ds
    @RISHURAJ-jd5ds Год назад +2

    bro just keep doing ur work.
    i appreciate the hard work that u put in to give us a detailed solution. 😊😊

  • @Momentsofmagic28
    @Momentsofmagic28 5 месяцев назад +1

    I saw multiple videos for this and referred discuss section of leetcode. This is by far the best explanation. Hats off

  • @aws_handles
    @aws_handles 8 месяцев назад +2

    Feels illegal to get such quality content free of cost 😅❤️

  • @harikeshsingh93
    @harikeshsingh93 Год назад +3

    bhaiya your content is amazing keep it up

  • @adarshgupta1972
    @adarshgupta1972 3 месяца назад

    Awesome Explanation!!

  • @pubgindia2003
    @pubgindia2003 5 месяцев назад +2

    Starting my day with your video great quality control brother ✨

  • @pranavkhatri30
    @pranavkhatri30 Год назад +2

    great explaination, bhaiya!

  • @shashankjoshi8250
    @shashankjoshi8250 5 месяцев назад +5

    You explained "what is the solution" really well. But you didn't explained, how to come up with this solution? What should be thought process behind this solution ?

    • @rahulnegi456
      @rahulnegi456 5 месяцев назад

      yeah the intution is missing, these subarray problems are bi*ch

  • @ugcwithaddi
    @ugcwithaddi 7 месяцев назад

    I agree with others that this is indeed a crystal clear explanation.

  • @RahulSeetharaman
    @RahulSeetharaman 5 месяцев назад +1

    Excellent explanation. Thank you !

  • @Brute_Coder
    @Brute_Coder Год назад +1

    I am grateful for your help 🙌🙌

  • @tanujamehra608
    @tanujamehra608 5 месяцев назад

    great explanation

  • @spdh6325
    @spdh6325 5 месяцев назад

    You are so Good. Your Explanation is amazing man! MIK.. from today i will be your daily User!

  • @girikgarg8
    @girikgarg8 11 месяцев назад +1

    I remember struggling with this question earlier. Such a nice explanation!

  • @ezcoding69
    @ezcoding69 Год назад +4

    Java Code for this solution is:
    class Solution {
    public long countSubarrays(int[] nums, int minK, int maxK) {
    long ans=0;
    int min_k_pos=-1,max_k_pos=-1,culprit=-1;
    for(int i=0;i

  • @deadsoul7702
    @deadsoul7702 5 месяцев назад +1

    thank you for such a good explanation!!

  • @AnandKumar-kz3ls
    @AnandKumar-kz3ls Год назад +1

    you made my day

  • @gauravbanerjee2898
    @gauravbanerjee2898 5 месяцев назад +1

    Thanks a lot bhaiya ❤❤ Got the Leetcode March badge all credit goes to you 😇😇❤❤

  • @gui-codes
    @gui-codes 5 месяцев назад

    super duper explanation man. loved how you break things and make even Hard look simple.

  • @UECAshutoshKumar
    @UECAshutoshKumar 5 месяцев назад +1

    Thank you 😊

  • @aadil4236
    @aadil4236 5 месяцев назад

    Thanks man, What a consice approach and explanation.

  • @tutuimam3381
    @tutuimam3381 Год назад +1

    Thanks a lot

  • @maihu2464
    @maihu2464 5 месяцев назад +2

    please could you explain how you got the intuition for the smaller - culprit, which will give a valid subarray, and how you got to know that you have to take a smaller

  • @abhishekverma7604
    @abhishekverma7604 Год назад +1

    great bhya , u made it look like it wasn't what it was.....
    my entire hostel is preparing from your dp playlist...bcoz it is that amazing..
    one request please add more variations to this playlist as well...

    • @codestorywithMIK
      @codestorywithMIK  Год назад +1

      Wow. It means a lot.
      Thank you so much.
      Sure, more coming ❤️❤️❤️

  • @edtrust
    @edtrust 5 месяцев назад +1

    This is what i understood:
    class Solution {
    public:
    long long countSubarrays(vector& nums, int minK, int maxK) {
    long long count = 0;
    // Tracks the last index of an element equal to minK encountered so far
    int lastMinIndex = -1;
    // Tracks the last index of an element equal to maxK encountered so far
    int lastMaxIndex = -1;
    // This variable will be used to mark the beginning of a new subarray
    // after encountering an element outside the range [minK, maxK]
    int startAgain = -1;
    for (int left = 0; left < nums.size(); ++left) {
    // If the current element is outside the desired range
    if (nums[left] < minK || nums[left] > maxK) {
    // Reset the starting point for the next subarray
    startAgain = left;
    } else {
    // Update lastMinIndex if the current element is equal to minK
    if (nums[left] == minK) {
    lastMinIndex = left;
    }
    // Update lastMaxIndex if the current element is equal to maxK
    if (nums[left] == maxK) {
    lastMaxIndex = left;
    }
    // Calculate the valid length of the current subarray
    // considering both minK and maxK elements We use max(0, ...) to
    // ensure we don't get a negative count (which wouldn't make
    // sense) min(lastMinIndex, lastMaxIndex) - startAgain
    // represents the number of valid elements in the subarray
    count += max(0, min(lastMinIndex, lastMaxIndex) - startAgain);
    }
    }
    return count;
    }
    };

  • @angomgeetchandra8127
    @angomgeetchandra8127 Год назад +1

    Really good. Thanks!

  • @Ankitkumar-fz3kc
    @Ankitkumar-fz3kc 5 месяцев назад +2

    Whenever i feel i get stuck in LC daily i always comes too your channel and boom magic happens everytime. Also need your help any medium through which i can talk to you as i tried topmate to get chance to talk with you but didn't find any meeting scheduling link. If possible provide any methods to talk to you personally.

    • @codestorywithMIK
      @codestorywithMIK  5 месяцев назад

      Let me soon reopen my topmate. Currently i have been very occupied as I have been shifting to a different city.

  • @bhargavijethva7958
    @bhargavijethva7958 5 месяцев назад

    Very very very nice explanation!👏😇

  • @pulastyadas3351
    @pulastyadas3351 Год назад +1

    so helpful

  • @akhilendradwivedi0code_x
    @akhilendradwivedi0code_x 5 месяцев назад

    A great approach

  • @teji644
    @teji644 5 месяцев назад

    Intuition:
    1. As you move index i, Think how many valid subarrays are ending at this position.
    2. Think what could be the shortest valid subarray (Subarray containing minK and maxK at the ends)
    3. Also subarray till i (current index) and including the minK and maxK is also valid. So we only care subarray from leftmost valid index to current index i.
    4. So to find leftmost we take min(minKPos,maxKPos).
    5. So now we just need to think how far we can extend from leftmost valid index so that we count remaining subarrays that are still valid.
    6. We can go left until we find an element that is out of bound (>maxk or

  • @Yash-sl9yp
    @Yash-sl9yp Год назад +15

    Bhaiya how one is supposed to think like that in an interview .....dimmag meh hi nhi aayega yeh toh :(

  • @vedanshgaur9517
    @vedanshgaur9517 Год назад +1

    Hands down best explanation. And welcome back brother hopefully everything is good now.

  • @smitpatel8361
    @smitpatel8361 Год назад +1

    You deserve more likes and subscribers.....

  • @mohithadiyal6083
    @mohithadiyal6083 Год назад

    Amazing explanation

  • @molyoxide8358
    @molyoxide8358 Год назад

    Feeling Happy that I got the approach.
    But, feeling Sad for not getting this approach on my own.

  • @ajit287
    @ajit287 Год назад

    Good Approach !!!

  • @drbullah1388
    @drbullah1388 Год назад +2

    Bhaiya, here we are using culprit index to bring out our answer.
    Say the array is 3,2,1,4,4,5,6 and minK = 1, maxK = 5
    So, when i = 5, that is where we directly add 3 to our answer which is required of us
    Now, what was the thought process behind using a culprit index, that too for evaluating the Ans?
    Like, if we wanted to give INT_MIN to all values
    Then, what would we do?

  • @shreshthkushwaha
    @shreshthkushwaha Год назад +1

    brilliant , Top G ! How did you come up with the approach?

    • @codestorywithMIK
      @codestorywithMIK  Год назад

      Thanks a lot .
      Just some dry run helped and also similar qns in past

  • @sparshyadav9709
    @sparshyadav9709 5 месяцев назад

    Understood.

  • @JJ-tp2dd
    @JJ-tp2dd Год назад +2

    Below is the JavaScript solution:
    var countSubarrays = function(nums, minK, maxK) {
    let ans = 0;
    let minKPosition = -1;
    let maxKPosition = -1;
    let culpritIndex = -1;

    for(let i=0; i maxK) {
    culpritIndex = i;
    }
    if(nums[i] == minK) {
    minKPosition = i;
    }
    if(nums[i] == maxK) {
    maxKPosition = i;
    }

    let smaller = Math.min(minKPosition, maxKPosition);
    let temp = smaller - culpritIndex;

    ans+= (temp

  • @ankanbrahmachary6581
    @ankanbrahmachary6581 5 месяцев назад

    class Solution:
    def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
    minPos = -1
    maxPos = -1
    i = -1
    res = 0
    for j in range(len(nums)):
    if nums[j]>maxK or nums[j]-1 and maxPos>-1:
    res += min(minPos, maxPos)-i
    return res
    I used this to solve the question .. but anyway I think its same as the 'i' here is the culprit index.. but only thing we dnt need to care about getting a negative count here in case of a invalid subarray ..
    i got this intuition after learning from you only .. thanks for all the awesome crystal clear explanations as always

  • @Thescienceworld652
    @Thescienceworld652 5 месяцев назад +1

    please make a video on : Minimize Manhattan Distances , leetcode weekly contest 391 4th problem

  • @wearevacationuncoverers
    @wearevacationuncoverers Год назад +1

    This is lit 🔥

  • @Buthkun
    @Buthkun 5 месяцев назад +2

    I think it is not possible to solve this problem without knowing this concept and you wont able to solve at first time if you won't solve such problem ever.

    • @apputadhiyal9451
      @apputadhiyal9451 5 месяцев назад +1

      yeah, this approach is super clean but feels like you have to remember it, or else you can't solve this problem in the future.

  • @prudhvirajmacherla9854
    @prudhvirajmacherla9854 Год назад +1

    Super hero

  • @AmanKumar-qz4jz
    @AmanKumar-qz4jz 5 месяцев назад

    bhaiya kaise soch lete ho aise kaise story bana lete ho......... thank you!!!!!

  • @nibaronkumar2484
    @nibaronkumar2484 Год назад

    Accha hain. Thanks bro

  • @piyushmalviya6543
    @piyushmalviya6543 5 месяцев назад

    i got march badge bro because of your op explaination

  • @knixkcodes
    @knixkcodes Год назад +1

    i still don't understand the formula fully. But great explanation overall

  • @sauravchandra10
    @sauravchandra10 5 месяцев назад

    Python code:
    class Solution:
    def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
    minIndex = maxIndex = culpritIndex = -1
    ans = 0
    for i in range(len(nums)):
    if nums[i] == minK:
    minIndex = i
    if nums[i] == maxK:
    maxIndex = i
    if nums[i] < minK or nums[i] > maxK:
    culpritIndex = i
    ans += max(0,min(minIndex,maxIndex)-culpritIndex)
    return ans

  • @abhijitroy1958
    @abhijitroy1958 5 месяцев назад

    class Solution {
    public:
    long long countSubarrays(vector& nums, int mini, int maxi) {
    int sz=nums.size();
    long long ref=0 ;
    long long cnt=-1;
    auto k=[&](int h, int l){
    queuemx;
    queuemn;
    for(int c=h;c

  • @AbhishekPandey-dj2eo
    @AbhishekPandey-dj2eo 5 месяцев назад +2

    Explaination is good, but how to approach this type of question, what concept are you using, if you use new concept for every question, how will i tackle new question.

    • @HimanshuPatel-wn6en
      @HimanshuPatel-wn6en 2 месяца назад +1

      Yes, he explains it well, but he doesn't cover the intuition or the approach to solving this type of question. If the question changes a bit, someone who has only watched this video might not be able to solve it. Prefer Striver for DSA as he explains the intuition very well.

  • @anshtanwar1813
    @anshtanwar1813 5 месяцев назад +1

    I could not understand the solution completely this is what I understood
    //make subarrays with maxIndex and minIndex in it
    //for example maxIndex = 2 and minIndex = 5
    //now number of subarrays till index 5 cannot be calculated as j-i+1 ? as all the other possibilities my exclude maxIndex element which is on index 2
    //simply we choose maxindex(smaller and on left of min Index) and find other subarrays on left of it till the culprit index is not found
    Is it correct?

  • @09avishkargaikwad71
    @09avishkargaikwad71 5 месяцев назад

    Outstanding explanation.
    Sir, do you come to this approach of culprit index because in first attempt it might be difficult to get this.

  • @tijuse
    @tijuse 5 месяцев назад

    Can you please explain why did you choose to take min of the maxKindex, minKindex? What is the idea behind this?

  • @aditigupta6870
    @aditigupta6870 3 месяца назад

    but why in both examples you considered the min element only at last index of array? It could have been in the middle or anywhere, and then we need to consider even the right part of array after that

  • @asheeshmishra5852
    @asheeshmishra5852 Год назад +1

    How one can think for this approach?? This Approach is really amazing. But How can I develop my mind to think like this ??

    • @atryshsharma773
      @atryshsharma773 Год назад

      this is the actual doubt I have anytime I have to watch his videos, how to build this approach?

    • @elakstein
      @elakstein Год назад

      @@atryshsharma773 this is a sliding window concept. Intution is nothing but pattern created by brain. The more you solve more easier it will become for your brain to come up with solutions like these.

  • @gurnoorchhabranit-jalandha5002
    @gurnoorchhabranit-jalandha5002 Год назад +1

    bhai playlist looa trees recursion ke upr please

  • @dpathak15
    @dpathak15 5 месяцев назад +1

    what if we had more than one culprit?

  • @VineetKumar-pj1bk
    @VineetKumar-pj1bk Год назад

    ❤️

  • @KeshavKumar-jl1ub
    @KeshavKumar-jl1ub 5 месяцев назад

    sri iska .... sliding window and queue ka use karke approach bata sakta hai ....

  • @anshror2583
    @anshror2583 2 месяца назад +1

    This solution cant be generated intuitively by me

  • @yuvhrajverma9665
    @yuvhrajverma9665 Год назад

    Sir can u plz explain intuition behind the smallest index - culprit index

    • @shreshthkushwaha
      @shreshthkushwaha Год назад

      The number of subarrays possible will be the number of elements between the culprit and the first maxK/minK element including itself. SO the smallestIndex - culprit will give you that number only.

  • @sauravchandra10
    @sauravchandra10 5 месяцев назад

    This is one of those problems where bhaiya ki approach samajne me dikkat aarhi hai. Anyone else?

    • @gui-codes
      @gui-codes 5 месяцев назад +1

      not me. Ek do jagah rewind karke dekha, ek do dry run kara. ache se samajh agaya.
      But tbh kaafi tough hai khud se aise approach soch paana. Unless the person has solved it before only then will be able to solve during interviews.

    • @sauravchandra10
      @sauravchandra10 5 месяцев назад

      Then maybe it is me

  • @ece116pranaykumar4
    @ece116pranaykumar4 Год назад +1

    bro can u solve one question of mine

  • @sridatreemukherjee371
    @sridatreemukherjee371 5 месяцев назад

    Brute force would not work as it exceeds time limit

  • @simnidhnidh9122
    @simnidhnidh9122 25 дней назад

    Please tell this one thing ki aapane story kaise banayiii i mean how u get this intuition?????

  • @viveksingh_01
    @viveksingh_01 5 месяцев назад

    What is the intuition for this approach

  • @karanmehta2890
    @karanmehta2890 5 месяцев назад

    Bhai yeh solution toh bohot zyaada acha hai, but yeh socha kaise? Intuition hi nahi aayi mujhe yeh sochne ki..

  • @clashofclansindiadevil5329
    @clashofclansindiadevil5329 21 день назад

    How can someone come with this solution, I am unable to get the institution.

  • @HimanshuPatel-wn6en
    @HimanshuPatel-wn6en 2 месяца назад

    Interview me ye approach mind me click bahut muskil hai 😥😥

  • @dhairyachauhan6622
    @dhairyachauhan6622 Год назад +1

    bhiya code tak toh theek hai but there is no why i can come up with this :( and i would never learn the code

    • @codestorywithMIK
      @codestorywithMIK  Год назад

      Hi there,
      Actually it’s alright. In the beginning i could also not come up on my own. I practiced such qns.
      So keep practising more and more. It will help a lot ❤️

  • @arshitcc
    @arshitcc 5 месяцев назад

    did microsoft asked this for SDE 1 ?

  • @utkarshsahay9908
    @utkarshsahay9908 5 месяцев назад

    Number of subarrays at index i ya at index j kitne honge ye kaise find karte hai sir ??? I mean formula and intuition ???

    • @codestorywithMIK
      @codestorywithMIK  5 месяцев назад

      To calculate the number of subarrays from index i that “end at index j”, you can use the formula:
      Number of subarrays = j - i + 1
      This formula works because for any subarray starting at index i and ending at index j, there are (j - i + 1) elements in that subarray.

    • @utkarshsahay9908
      @utkarshsahay9908 5 месяцев назад

      @@codestorywithMIK Thanks ❤❤

  • @kapilnitb
    @kapilnitb 5 месяцев назад

    Attempt krke aata hu agar nhi hua to

  • @mertyertugrul
    @mertyertugrul Год назад

    Is this in English or not. If not why all the titles and comments are in English?

    • @AnuragSingh-xe1nm
      @AnuragSingh-xe1nm 13 дней назад

      Because the question title on leeetcode is in english.

  • @molyoxide8358
    @molyoxide8358 Год назад

    Bro aisa approach kasie aapke dimag mein aaya??