Sum of Subarray Minimums | Detailed | Leetcode 907

Sdílet
Vložit
  • čas přidán 24. 11. 2022
  • Whatsapp Community Link : www.whatsapp.com/channel/0029...
    This is the 4th Video of our Playlist "Stack : Popular Interview Problems".
    In this video we will try to solve a very good and famous stack problem - Sum of Subarray Minimums (Leetcode 907)
    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 : Sum of Subarray Minimums (Leetcode 907)
    Company Tags : Paytm
    My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/sum-of-...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    Subscribe to my channel : / @codestorywithmik
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Approach Summary : The approach involves finding the next smaller element to the left (NSL) and the next smaller element to the right (NSR) for each element in the array. It utilizes a stack-based approach to efficiently compute these values.
    The algorithm then iterates through each element of the array, calculating the distance to the nearest smaller element on the left (d1) and the distance to the nearest smaller element on the right (d2). For each element, it determines the total number of ways to form subarrays with that element as the minimum by multiplying d1 and d2. The sum of the minimum elements for each subarray is accumulated, and the result is returned after taking the modulo operation with 1e9 + 7.
    The code leverages the concept of finding the next smaller element to efficiently compute the sum of minimum elements for all possible subarrays in a given array.
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    ✨ 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 #2024 #newyear

Komentáře • 251

  • @varunbhargavmoori7815
    @varunbhargavmoori7815 Před rokem +72

    This is the only perfect video for this problem in the whole internet.

  • @shadowgamer8105
    @shadowgamer8105 Před 3 dny +1

    Sir love you dil se sachhi bole rahe hai aapke jaisa bhot kam log free mein intuition provide krta hai literally thank you sir 👌👌

  • @harikuduva
    @harikuduva Před 6 měsíci +16

    I don't even speak hindi and I understood your explanation more than those other videos explaining in english. Great work.

  • @aryansinha1818
    @aryansinha1818 Před 5 měsíci +4

    Man who are you, "Best explainer I guess". How can you explain so good. Hats off to you. Thank you from bottom of my heart. Love and respect always. It felt like I am binge watching Netflix. It was that smooth and crisp and clear.

  • @nagmakhan672
    @nagmakhan672 Před rokem +6

    There is literally no video which gone to this depth for this Qn
    Thanks a lot 😊

  • @Rajsingh-bh4qw
    @Rajsingh-bh4qw Před rokem +14

    literally i watched so many videos but couldnt find a better explanation than this one. Thanks a lot sir🙏🙏🙏 each and every doubt is clear and also the edge cases.

  • @codestorywithMIK
    @codestorywithMIK  Před rokem +14

    If you are wondering how we come up with the formula at 12:06 -
    Let's take an example shown below :
    _, _, _, 2, _, _
    If you notice-
    No. of ways to start our subarray from left of 2 (including 2) is = 4 (i.e. we have 4 possible starting points)
    No. of ways to end our subarray at right of 2 (including 2) is = 3 (i.e. we have 3 possible endings for our subarray)
    As per above two equations, For each starting point we have 3 ending points possible
    Total = 4*3 = 12

    • @RaviTeja-cq6pu
      @RaviTeja-cq6pu Před 8 měsíci

      _, _, _, 2, _, _
      how is it possible to start from 2. could you please explain.......

    • @anuragv400
      @anuragv400 Před 7 měsíci

      @@RaviTeja-cq6pu take one scenario where you started and ended on 2.

    • @fettuccine794
      @fettuccine794 Před 6 měsíci

      This is the exact thing I was searching for in the entire internet, thank you So much ❤❤❤❤

    • @user-do1eq7tl3e
      @user-do1eq7tl3e Před 6 měsíci +2

      still if u find it confusing , here is simple derivation.
      let the number of elements before given element (eg: 2) be "b", and after given element (eg: 2) be "a",
      so , total number of subarray will be
      b // [3,4,5, 2] , [4,,5,2] , [5,2]
      a // [2,3,4], [2,3]
      1 // [2]
      b*a // 9:07 cases
      so equation is b + a + 1 + (b*a) .. if u solve this its equal to (b+1)*(a+1)

  • @pranavkhatri30
    @pranavkhatri30 Před rokem +9

    Till now, I have watched multiple videos for this concept. Couldn't find better explanation than this. Worth watching!

  • @Newbie789
    @Newbie789 Před rokem +8

    bahut clear hai concept is video ke baad, thanks you so much. shaandar jabardust dindabaad😀

  • @ramprasath3818
    @ramprasath3818 Před 6 měsíci +5

    This channel is gold. The way he explains it step by step by walking through what he thinks, is really great. And I don't even know Hindi lol. Thanks a lot.

  • @gui-codes
    @gui-codes Před 15 dny

    Bekar hi itne videos dekhe. Pehle hi is channel me ajana chaie tha.
    Bhai tumse acha isko koi nahi explain kar sakta. hats off to you

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

    Intuition.... is the main thing that way you have explained.

  • @aadil4236
    @aadil4236 Před 17 dny +1

    Very Good explanation brother. I was only able to get it after watching your explanation. Kudos to you!

  • @Deadinside567
    @Deadinside567 Před rokem +10

    You are creating a gold mine for future programmers ❤❤ loved the way of explanations
    Zero rote learning only concept ❤

  • @harshitgupta8089
    @harshitgupta8089 Před rokem +4

    Bhaiya I love you Your way of explaination is awesome
    I GOT THE BEST EXPLAINATION HERE FINALLY

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

    That's called a mindblowing explanation!!!
    Amazed at How I stayed here for 49 minutes, It was worthwhile.

  • @technologicalvivek7510
    @technologicalvivek7510 Před 4 měsíci +1

    mene 3,4 videos dekhe youtube pe iss question ka point of view samajhne ke liye but ek bhi youtuber depth me bata nhi paye ,jis depth me aapne bataya.
    Thank you.

  • @itssumit6161
    @itssumit6161 Před 3 měsíci +1

    You are just awesome sir u made the question more easy with your explanation. ❤

  • @this_is_ur_ms
    @this_is_ur_ms Před 4 měsíci +1

    Great Explanation, cleared everything. Thankyou.

  • @niteshk0487
    @niteshk0487 Před rokem +4

    Bhai God Level explanation❤✅

  • @souravjoshi2293
    @souravjoshi2293 Před rokem +8

    First , your voice just makes me feel that the Qn is too easy and the way you explain , everything seems so effortless.I am still speechless with the amount of effort you put in a video. Thanks and congratulations for being such an impactful tutor. I can sense of urge in your voice to transfer the knowledge to us.
    People will soon start knowing you from this channel.
    Thanks from me and my friends in college who have started following you

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +2

      This comment made my day. Thanks a lot ❤️❤️❤️

    • @shikharmalik3787
      @shikharmalik3787 Před rokem +3

      this guy got some great voice...every question feels easy after that///

  • @wearevacationuncoverers
    @wearevacationuncoverers Před rokem +1

    This is the only best explanation I found for this problem. You are too good.
    Let's support this Legend and make him famous.

  • @VedBrahmbhattBEE
    @VedBrahmbhattBEE Před 6 měsíci

    Best Explanation for this problem!! Thanks Sir : )

  • @varunsheth1003
    @varunsheth1003 Před rokem +2

    Nice Explanation

  • @joydeep-halder
    @joydeep-halder Před 6 měsíci

    Such a detailed explanation! Hats Off 🔥🔥 Earned a subscriber!

  • @tsukuruuu
    @tsukuruuu Před 11 měsíci +1

    Great content as usual. Whenever i search any question and notice that you have made a video on that, i immediately click on it without a second doubt , no matter how long the video. Please don't stop making and uploading such videos! You truly are a gem! Thank you sm!

  • @vishalgaurav4193
    @vishalgaurav4193 Před 6 měsíci

    Now I finally subscribed your channel after this great explanation👍.Really impressed with the way you explain this problem easily

  • @arthurlewin1952
    @arthurlewin1952 Před rokem +2

    Amazing explaination sir solve largest rectangular area of histogram using this

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +3

      Sure Arthur.
      Thanks a lot
      I will soon upload Histogram one

  • @MakeuPerfect
    @MakeuPerfect Před rokem +3

    You are superb

  • @tusharbhatia5437
    @tusharbhatia5437 Před 6 měsíci

    Beautiful explanation. Was able to code myself after your explanation itself thanks !

  • @rounaq_khandelwal
    @rounaq_khandelwal Před 11 měsíci +1

    You are absolutely right, others have directly written the code w/o proper explaination!! Thanks a lot Mik Bhai for this wonderful video

  • @pankaj_2003
    @pankaj_2003 Před 5 měsíci

    best explaination i hv got fr this question ,thankyou so much ........

  • @riyadhossain1706
    @riyadhossain1706 Před rokem +1

    Literally, amazed by the way you elaborate on the solution.

  • @Abhay14
    @Abhay14 Před 6 měsíci +1

    thanks a lot bhaiya , sayad hi aapse acchha koi iss question ko samjha skta tha this topic was so deep and interested

  • @robot3.077
    @robot3.077 Před 4 měsíci +1

    best explanation on youtube.
    fell in love with your explanation🙏🙏😍😍

  • @footballalliance2412
    @footballalliance2412 Před 5 měsíci

    Thank you so much sir, now i got the intuition.

  • @Thriftinghai
    @Thriftinghai Před rokem +1

    One of the best explanations

  • @amlantribedi7954
    @amlantribedi7954 Před 7 měsíci +1

    By Far the best Explanation in CZcams !

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

    No one, I repeat no one can teach like this LEGEND

  • @akshitatyagi04
    @akshitatyagi04 Před 2 měsíci +1

    Best Explaination!

  • @md.tamaltahasinkhan6448
    @md.tamaltahasinkhan6448 Před 6 měsíci

    what a detailed explanation!!! ❤

  • @footballcreativeeverywhere260
    @footballcreativeeverywhere260 Před 6 měsíci +1

    Great mik bhai❤

  • @SurajKumar-ku1lg
    @SurajKumar-ku1lg Před 2 měsíci

    best video explanation of that problem

  • @adityaparab4314
    @adityaparab4314 Před 6 měsíci

    Best Explaination!!

  • @chiragsharma8905
    @chiragsharma8905 Před 7 měsíci +1

    Subscribed❤. Loved the explanation.

  • @medhanshagrawal5787
    @medhanshagrawal5787 Před 2 měsíci +1

    Just Perfect Broo.
    Loved it❤

  • @reemakumari9495
    @reemakumari9495 Před rokem +1

    love you sir ...finally abb samajh aaya

  • @it17harshahuja81
    @it17harshahuja81 Před 9 měsíci +2

    Liked only because of the Edge Case👍👍👍👍

  • @nitingoel258
    @nitingoel258 Před 3 měsíci +1

    Very well explanation !

  • @eng.gaming5557
    @eng.gaming5557 Před 6 měsíci +2

    Perfection belongs to one and only Mazhar Khan Sir...
    Thanks for ur efforts

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

    Thankyou Mik for awesome content

  • @adityatomar9820
    @adityatomar9820 Před 5 měsíci +1

    god level explanation bhaii...i was struggling so much in this ques

  • @hemantkamath5841
    @hemantkamath5841 Před 5 měsíci

    Thank you so much MIK bhai 🙌

  • @TejalKumawat-hu9yi
    @TejalKumawat-hu9yi Před 3 měsíci +1

    best explaination on the internet for this question

  • @ankitasantape1063
    @ankitasantape1063 Před 6 měsíci +1

    Thanks a lot Sir for this wonderful in depth explanation.

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

    Best on youtube ❤

  • @ritikabisht2055
    @ritikabisht2055 Před 4 měsíci +1

    Really nice and detailed explanation ❤

  • @sjxsubham...
    @sjxsubham... Před 6 měsíci

    best.... solution... thank MIK sir

  • @souravsingh9993
    @souravsingh9993 Před rokem +1

    Thanks bro.

  • @anveshasharma2726
    @anveshasharma2726 Před 2 měsíci +1

    very nyc explaination. support from haryana

  • @Siddharth1924
    @Siddharth1924 Před 2 měsíci +1

    top notch!!

  • @shifuology_yt
    @shifuology_yt Před 7 měsíci +1

    Wow! Thank you so much!!

  • @as2002ajaysingh
    @as2002ajaysingh Před 5 měsíci

    thanks bhai best explanation. on yt

  • @SamsIt-hi9gf
    @SamsIt-hi9gf Před měsícem +1

    bhai your explaination is op....

  • @shrijonbiswas8105
    @shrijonbiswas8105 Před rokem +1

    U are a genius..keep making videos for such lesser known problems

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      It means a lot 🙏🙏❤️❤️😇😇
      Sure I will be posting more

  • @ANNAM-dv5dy
    @ANNAM-dv5dy Před 3 měsíci

    good explanation brother thanks

  • @sayantanpoddar5428
    @sayantanpoddar5428 Před 6 měsíci +1

    I search all over internet for those videos which have brute force solution .....and after that i found this channel and here only i got step by step thinking towards optimal solution ............near about 60 days i follow this channel and explainations are really great

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

    That Was Damn Clean and Good Explanation Bro !!

  • @DR-mq1le
    @DR-mq1le Před 11 měsíci +1

    great explanation! thanks

  • @rahulverma9237
    @rahulverma9237 Před 11 měsíci +1

    beautifully explained, solved the sum of subarray ranges question by this method, keep up the good work

  • @parvahuja7618
    @parvahuja7618 Před 25 dny +1

    Thankyou so much sir

  • @theshlok
    @theshlok Před 6 měsíci

    Probably the only video which is able to actually explain the intuition behind this problem.

  • @ugcwithaddi
    @ugcwithaddi Před 6 měsíci

    Dope explanation
    Best one on internet 🔥

  • @jayashankarmaddipoti6964
    @jayashankarmaddipoti6964 Před 6 měsíci +2

    As usual, I was able to solve using a brute force approach and received a time limit exceeded error. No one born with enormous reasoning/analytical skills so as I. No need to worry, check who else solved the problem before, understand the approach and follow it. There is no mistake in following others when you are not sure. Thanks, MIK bro. No stop to learning. One day, I will become a pro of programming.

  • @Raj10185
    @Raj10185 Před rokem +1

    This question is really amazing bas observation jaise kiya nsl and nsr se ho jyega bas question is very much standard bas formula figure out me thoda dikkat hua baki ab sab smoothly solve ho gya.

  • @viidhudubey8573
    @viidhudubey8573 Před rokem +1

    very well and thoroughly explained, Thank you for this

  • @stepup7052
    @stepup7052 Před rokem +2

    thanks bhaia

  • @shivamnirmalkar6251
    @shivamnirmalkar6251 Před 18 dny

    //vector getNSL(vector& arr, int n) , bhaiya aapne yaha NSL likh rakha hai or aap for loop se travel kr rhe right side //for(int i = 0; i

  • @nikhilsatyam4815
    @nikhilsatyam4815 Před 6 měsíci +1

    awesome exlaination after striver you are the best tutor on youtube for dsa.

  • @dyashwanth9822
    @dyashwanth9822 Před 6 měsíci +1

    Thank You so much bhai , Very well explained ❤

  • @YashSinghal
    @YashSinghal Před rokem +4

    12:06 how did you think of this formula? I tried a lot to figure a similar formula

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +11

      Let's take an example shown below :
      _, _, _, 2, _, _
      If you notice-
      No. of ways to start our subarray from left of 2 (including 2) is = 4 (i.e. we have 4 possible starting points)
      No. of ways to end our subarray at right of 2 (including 2) is = 3 (i.e. we have 3 possible endings for our subarray)
      As per above two equations, For each starting point we have 3 ending points possible
      Total = 4*3 = 12

    • @wearevacationuncoverers
      @wearevacationuncoverers Před rokem

      @@codestorywithMIK you are a magician

  • @AnjuGupta-sz1uk
    @AnjuGupta-sz1uk Před 6 měsíci

    very clear explanation for each and every point of the problem...bhaiya your explanation is crystal clear ,like no need to go for any other videos for understanding the concept after watching your video.

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před rokem +1

    Very beautiful explanation and much intuitive thanks

  • @Kings-ik9lh
    @Kings-ik9lh Před 6 měsíci

    we can also check left and right elements at the travrsal time of index also but it will take Time complexity O(N^2) but space complexity will be O(1)
    int mod = 1e9 + 7;
    int sumSubarrayMins(vector& arr) {
    long long ans = 0;
    for(int i=0;i=0 && arr[j]>=temp){
    leftcount++;
    j--;
    }
    int k = i + 1;
    long long rightcount = 0;
    while(ktemp){
    rightcount++;
    k++;
    }
    ans=(ans%mod+(leftcount+1)%mod*(rightcount+1)%mod*temp%mod)%mod;
    }
    return (ans % mod);

  • @ss8273
    @ss8273 Před rokem +1

    thank you sir keep it up really helpful........😍😍

  • @sohamjoshi25
    @sohamjoshi25 Před 6 měsíci +1

    Great Video!!!! Keep it up.

  • @NatureLover-oq6uc
    @NatureLover-oq6uc Před rokem +2

    Great explanation....

  • @ujjawaldewangan8345
    @ujjawaldewangan8345 Před rokem

    bahut E badhiya

  • @user-iv3jg1jn9z
    @user-iv3jg1jn9z Před 4 měsíci +1

    thanks bro loved it !Thanks😘😘😘😘😘😍 Enjoyed it Dil de

  • @shubhamkewat2496
    @shubhamkewat2496 Před rokem +2

    I Appreciate it.

  • @RV-qf1iz
    @RV-qf1iz Před 10 měsíci +2

    thanks bro i was able to solve this problem on my own after understanding the logic.

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

    Thanks a lot bhaiya ❣️ this was the best explanation for this question among all of the other explanations I have seen so far

  • @tusharyadav5874
    @tusharyadav5874 Před 6 měsíci +1

    Great explanation bhai :)

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

    I tried to do this question using Sliding Window technique and following is the code that I have written :
    class Solution {
    public:
    int sumSubarrayMins(vector& arr) {
    vector result;
    deque deq;
    int n = arr.size();
    int k = 1;
    while(k

  • @user-fs8km9qc8f
    @user-fs8km9qc8f Před 5 měsíci

    very nice

  • @ummedali5927
    @ummedali5927 Před 6 měsíci +1

    Great explanation ❤

  • @viveksingh4801
    @viveksingh4801 Před 9 měsíci

    SImply awesome !!

    • @codestorywithMIK
      @codestorywithMIK  Před 9 měsíci

      Thanks a lot! 🙏😇

    • @codestorywithMIK
      @codestorywithMIK  Před 9 měsíci

      Today’s upload will be a little delayed because I am travelling. But I have provided the link of my GitHub code (exact copy of LCS code pattern) in my post in CZcams
      Thank you 🙏😇

  • @manojyadav6967
    @manojyadav6967 Před rokem +1

    awesome exolanation !!!

  • @codingdev4196
    @codingdev4196 Před 6 měsíci +1

    You are gem 💎 brother

  • @sabbulingineni
    @sabbulingineni Před 6 měsíci

    Great video ❤
    Pls do videos on solid principles and design patterns pls 🙏🙏🙏