Sum of Subarray Minimums | Detailed | Leetcode 907

  Рет қаралды 24,274

codestorywithMIK

codestorywithMIK

Күн бұрын

Whatsapp Community Link : www.whatsapp.c...
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/MAZ...
Leetcode Link : leetcode.com/p...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My GitHub Repo for interview preparation : github.com/MAZ...
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

Пікірлер: 264
@varunbhargavmoori7815
@varunbhargavmoori7815 Жыл бұрын
This is the only perfect video for this problem in the whole internet.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much ❤️
@lalitk
@lalitk Жыл бұрын
Right 😊😊😊❤❤
@sehajdhawan580
@sehajdhawan580 Жыл бұрын
Right I watched so many videos before coming to this due to its length 😅
@technologicalvivek7510
@technologicalvivek7510 7 ай бұрын
@@sehajdhawan580 Right
@gui-codes
@gui-codes 2 ай бұрын
agree
@codestorywithMIK
@codestorywithMIK Жыл бұрын
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 10 ай бұрын
_, _, _, 2, _, _ how is it possible to start from 2. could you please explain.......
@anuragv400
@anuragv400 10 ай бұрын
@@RaviTeja-cq6pu take one scenario where you started and ended on 2.
@fettuccine794
@fettuccine794 8 ай бұрын
This is the exact thing I was searching for in the entire internet, thank you So much ❤❤❤❤
@10minutes_cs
@10minutes_cs 8 ай бұрын
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)
@Rajsingh-bh4qw
@Rajsingh-bh4qw Жыл бұрын
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 Жыл бұрын
Thank you Raj 😇💕
@bhuppidhamii
@bhuppidhamii 8 ай бұрын
when i did this Q's 3-4 weeks ago I fully understood it. but now today when i'm doing POTD I still cannot able to come back that stack will be used in this Q's. Why is it happening, what to do to prevent this?
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Hi there, It happens with me also. Don’t worry. I usually either try it for 30 minutes, if I don’t get it, i watch the solution and try to figure what exactly i was missing. Remember one thing : For any qn, firstly try the worst solution- Brute Force, then go to optimal. It helps you understand what was the reason that was causing the high time complexity in brute force, what exactly we should improve to make it optimal. This helps a lot. And keep practising more and more.
@bhuppidhamii
@bhuppidhamii 8 ай бұрын
@@codestorywithMIK thank you for your kind words
@YashSinghal
@YashSinghal Жыл бұрын
12:06 how did you think of this formula? I tried a lot to figure a similar formula
@codestorywithMIK
@codestorywithMIK Жыл бұрын
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 Жыл бұрын
@@codestorywithMIK you are a magician
@abhishekmittal1302
@abhishekmittal1302 8 ай бұрын
Java Solution!!! class Solution { int[] getleft(int arr[], int n){ int res[] = new int[n]; Stack stk = new Stack(); for(int i=0;iarr[i]){ stk.pop(); } res[i] = stk.isEmpty()? -1 : stk.peek(); stk.push(i); } return res; } int[] getright(int arr[], int n){ int res[] = new int[n]; Stack stk = new Stack(); for(int i=n-1;i>=0;i--){ while(!stk.isEmpty() && arr[stk.peek()]>=arr[i]){ stk.pop(); } res[i] = stk.isEmpty()? n : stk.peek(); stk.push(i); } return res; } public int sumSubarrayMins(int[] arr) { int n = arr.length; int left[] = getleft(arr,n); int right[] = getright(arr,n); long sum = 0; long M = 1000000007L; for(int i = 0;i
@harikuduva
@harikuduva 8 ай бұрын
I don't even speak hindi and I understood your explanation more than those other videos explaining in english. Great work.
@aws_handles
@aws_handles 8 ай бұрын
😮😮😮
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
You made my day. Thank you so much ❤️❤️🙏🙏
@footballcreativeeverywhere260
@footballcreativeeverywhere260 8 ай бұрын
East or west hindi is the best❤️😂
@RajanSingh-xr7cf
@RajanSingh-xr7cf 14 күн бұрын
oh angrej ke chode ................ jyada gyan mt de bhai ko hindi aati ni haii ....
@pranavkhatri30
@pranavkhatri30 Жыл бұрын
Till now, I have watched multiple videos for this concept. Couldn't find better explanation than this. Worth watching!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Pranav ❤️❤️❤️
@Danish-saifi1
@Danish-saifi1 Жыл бұрын
You are creating a gold mine for future programmers ❤❤ loved the way of explanations Zero rote learning only concept ❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much ❤️
@aryansinha1818
@aryansinha1818 7 ай бұрын
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.
@Newbie789
@Newbie789 Жыл бұрын
bahut clear hai concept is video ke baad, thanks you so much. shaandar jabardust dindabaad😀
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 😇🙏
@sz.110
@sz.110 Жыл бұрын
*New Test cases Has Been Added* Overflow aaraha hai toh *long long* ka use karein
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks Sajjad ❤️❤️
@nagmakhan672
@nagmakhan672 Жыл бұрын
There is literally no video which gone to this depth for this Qn Thanks a lot 😊
@harshitgupta8089
@harshitgupta8089 Жыл бұрын
Bhaiya I love you Your way of explaination is awesome I GOT THE BEST EXPLAINATION HERE FINALLY
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so so much ❤️❤️
@ramprasath3818
@ramprasath3818 8 ай бұрын
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.
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Thank you 😇🙏❤️
@AMANRAJ-dt8gu
@AMANRAJ-dt8gu 16 күн бұрын
The nicest explanation I have got as compared to other videos. Doing great job man🙏
@aadil4236
@aadil4236 2 ай бұрын
Very Good explanation brother. I was only able to get it after watching your explanation. Kudos to you!
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
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 Жыл бұрын
This comment made my day. Thanks a lot ❤️❤️❤️
@shikharmalik3787
@shikharmalik3787 Жыл бұрын
this guy got some great voice...every question feels easy after that///
@shristisrivastava7054
@shristisrivastava7054 18 күн бұрын
You always explain the logic so well. Hats off to you!
@subhajitdey135
@subhajitdey135 Ай бұрын
Bhaiya how to figure out this edge case jaisa ki is question me, question solve karte time to ya edge case dimag me nhi aya.
@deveshsharma-u2l
@deveshsharma-u2l 6 ай бұрын
thanks bro loved it !Thanks😘😘😘😘😘😍 Enjoyed it Dil de
@saurabhchaudhari4157
@saurabhchaudhari4157 3 ай бұрын
class Solution { private: vector prevSmaller(vector&arr){ int n=arr.size(); vectorans(n); stackst; for(int i=0;ielm){ st.pop(); } //empty if(st.empty()){ ans[i]=-1; } else{ ans[i]=st.top(); } st.push(i); } //push return ans; } vector nextSmaller(vector&arr){ int n=arr.size(); vectorans(n); stackst; for(int i=n-1;i>=0;i--){ int elm=arr[i]; //pop while(!st.empty() && arr[st.top()]>=elm){ st.pop(); } //empty if(st.empty()){ ans[i]=-1; } else{ ans[i]=st.top(); } //push st.push(i); } return ans; } public: int sumSubarrayMins(vector& arr) { int n=arr.size(); int MOD = 1000000007; vectornext=nextSmaller(arr); vectorprev=prevSmaller(arr);; long long sum=0; for(int i=0;i
@rajubali5627
@rajubali5627 16 күн бұрын
super sir...this is perfect explanation......
@rahuldwarijadavpuruniversi9322
@rahuldwarijadavpuruniversi9322 8 ай бұрын
I'm confused about one thing sir - we are calculating how many times an element come out as minimum in different subarrays and then adding them by multiplying the element. But how do we conclude that this approach will cover all the subarrays ? I mean how they are equivalent ?
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Let’s understand with an example. Suppose we write all the subarrays in which each element nums[i] is minimum. [17 , 55 , 82 , 55] Let's write for 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] Let's write for 55: [55], [55, 82], [55, 82, 55] -----> Keep an eye on this Let's write for 82 : [82] Let's write for 55 : [55], [82, 55], [55, 82, 55] ---> NOTE here [55, 82, 55] is duplicate when we wrote for last time 55 above. To avoid the duplicate I mentioned above. In one of the calculations, NSR or NSL, don't put >= So, for last 55, we will get [55], [82, 55] Now, start calculating : For 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] - 4 subarrays in which 17 is minimum so, result = 4*17 For 55: [55], [55, 82], [55, 82, 55] - 3 subarrays in which 55 is minimum result += 3*55 For 82 : [82] - 1 subarray in which 82 is minimum result += 1*82 For last 55 [55], [82, 55] - 2 unique subarrays in which this 55 is minimum result += 2*55 Total = 68 + 165 + 82 + 165 = 425 Hope this helps everyone. Thank you ❣
@rahuldwarijadavpuruniversi9322
@rahuldwarijadavpuruniversi9322 8 ай бұрын
@@codestorywithMIK Clear !! Thank you sir ❤
@itssumit6161
@itssumit6161 5 ай бұрын
You are just awesome sir u made the question more easy with your explanation. ❤
@shubhamagarwal8297
@shubhamagarwal8297 8 ай бұрын
duplicate case - [1,2,2] (makes us think)
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Indeed
@akshatmishra9574
@akshatmishra9574 3 ай бұрын
That Was Damn Clean and Good Explanation Bro !!
@iayushsharma
@iayushsharma 3 ай бұрын
Thankyou Mik for awesome content
@niteshk0487
@niteshk0487 Жыл бұрын
Bhai God Level explanation❤✅
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Nitesh ❤️❤️❤️
@arthurlewin1952
@arthurlewin1952 Жыл бұрын
Amazing explaination sir solve largest rectangular area of histogram using this
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure Arthur. Thanks a lot I will soon upload Histogram one
@khitijkarmakar
@khitijkarmakar 11 ай бұрын
definitely not a medium , perfect hard problem.
@Afiniciado
@Afiniciado Ай бұрын
best explanation found so far!!!
@RajanSingh-xr7cf
@RajanSingh-xr7cf 14 күн бұрын
sach mai great explation _____________
@bhuppidhamii
@bhuppidhamii 9 ай бұрын
corner case 35:35
@SamsIt-hi9gf
@SamsIt-hi9gf 3 ай бұрын
bhai your explaination is op....
@it17harshahuja81
@it17harshahuja81 11 ай бұрын
Liked only because of the Edge Case👍👍👍👍
@shadowgamer8105
@shadowgamer8105 2 ай бұрын
Sir love you dil se sachhi bole rahe hai aapke jaisa bhot kam log free mein intuition provide krta hai literally thank you sir 👌👌
@dumbstonks
@dumbstonks 29 күн бұрын
Bhai don't ever stop uploading such detailed intuitive videos, these are like finding gems in a junkyard
@medhanshagrawal5787
@medhanshagrawal5787 4 ай бұрын
Just Perfect Broo. Loved it❤
@harshtiwari416
@harshtiwari416 8 ай бұрын
Bhai aap keh rahe ho ki duplicate add hojayenge but agar test case [17 , 55 , 82 , 55] isko bina >= ke kru toh sum kum aarha hai 483 na ki zyada sahi answer se 593 kindly explain
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
The correct answer is 425. Let me explain how. First, write all the subarrays in which each element nums[i] is minimum. [17 , 55 , 82 , 55] Let's write for 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] Let's write for 55: [55], [55, 82], [55, 82, 55] -----> Keep an eye on this Let's write for 82 : [82] Let's write for 55 : [55], [82, 55], [55, 82, 55] ---> NOTE here [55, 82, 55] is duplicate when we wrote for 55 above. To avoid the duplicate I mentioned above. In one of the calculations, NSR or NSL, don't put >= So, for last 55, we will get [55], [82, 55] Now, start calculating : For 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] - 4 subarrays in which 17 is minimum so, result = 4*17 For 55: [55], [55, 82], [55, 82, 55] - 3 subarrays in which 55 is minimum result += 3*55 For 82 : [82] - 1 subarray in which 82 is minimum result += 1*82 For last 55 [55], [82, 55] - 2 unique subarrays in which this 55 is minimum result += 2*55 Total = 68 + 165 + 82 + 165 = 425 Hope this helps everyone. Thank you ❣
@ritikabisht2055
@ritikabisht2055 6 ай бұрын
Really nice and detailed explanation ❤
@robot3.077
@robot3.077 7 ай бұрын
best explanation on youtube. fell in love with your explanation🙏🙏😍😍
@codestorywithMIK
@codestorywithMIK 7 ай бұрын
🙏🙏❤️❤️
@prathamtetgure2963
@prathamtetgure2963 Жыл бұрын
Guys 50min jyada lagega dekhlo worth hai, dusri video dekhne ki jarurat nahi padegi
@codestorywithMIK
@codestorywithMIK Жыл бұрын
It means a lot 🙏😇❤️
@subhamoysasmal8382
@subhamoysasmal8382 19 күн бұрын
sir you are simply great
@Raj10185
@Raj10185 Жыл бұрын
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.
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
indeed
@MakeuPerfect
@MakeuPerfect Жыл бұрын
You are superb
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much ❤️❤️❤️
@dyashwanth9822
@dyashwanth9822 8 ай бұрын
Thank You so much bhai , Very well explained ❤
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
❤️🙏
@Rajan-xv9cz
@Rajan-xv9cz 5 ай бұрын
genvin
@nitingoel258
@nitingoel258 5 ай бұрын
Very well explanation !
@ankitasantape1063
@ankitasantape1063 8 ай бұрын
Thanks a lot Sir for this wonderful in depth explanation.
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
You are most welcome 🙏
@dayashankarlakhotia4943
@dayashankarlakhotia4943 8 ай бұрын
public int sumSubarrayMins(int[]arr){ int mod=1000000007; int[]st=new int[arr.length+1]; int idxSt=0; long ans=0; for(int i=1;i=0&&(i==arr.length||arr[st[idxSt]]>arr[i])){ int idx=st[idxSt--]; int leftIdx =idxSt
@jagdishkhetre4515
@jagdishkhetre4515 Ай бұрын
Thank you so much for detailed Explaination ...[ Formula :-> i - NSL[i] * NSR[i] - i ]
@ankitgupta7467
@ankitgupta7467 3 ай бұрын
That's called a mindblowing explanation!!! Amazed at How I stayed here for 49 minutes, It was worthwhile.
@parvahuja7618
@parvahuja7618 3 ай бұрын
Thankyou so much sir
@anveshasharma2726
@anveshasharma2726 4 ай бұрын
very nyc explaination. support from haryana
@codestorywithMIK
@codestorywithMIK 4 ай бұрын
🙏🙏❤️❤️😊😊
@akshitatyagi04
@akshitatyagi04 4 ай бұрын
Best Explaination!
@jambajuice07
@jambajuice07 8 ай бұрын
left + 1 * right +1 how u thought of this ? i was doing something like left + right + left*right ( combination ) but got wrong answer
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Actually i ran some examples before confirming the formula.
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
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
@ss8273
@ss8273 Жыл бұрын
thank you sir keep it up really helpful........😍😍
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Most welcome❤️❤️
@footballcreativeeverywhere260
@footballcreativeeverywhere260 8 ай бұрын
Great mik bhai❤
@pankaj_2003
@pankaj_2003 7 ай бұрын
best explaination i hv got fr this question ,thankyou so much ........
@Er0Shara
@Er0Shara 8 ай бұрын
nice explanation, always satisfying to see a solution coming from intuition. Thanks Mazhar. One small typo, i think while filling NSR via stack at time syamp kzbin.info/www/bejne/foO0c2pjeZeVn5I , I think at last instead of 3 , index 0 should be pushed in stack. But yeah question trick is quite confusing , but the ay to make it understand is really helpful, a lot of effort has been put in, in making this video they way it is, hence understandable with some typos.
@tryin_to_code
@tryin_to_code 4 ай бұрын
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
@parassaini8601
@parassaini8601 Жыл бұрын
Hello Sir, could you please solve Range Module question!
@newBieGuy05
@newBieGuy05 Жыл бұрын
your explanation is awesome , I understood in one go,, It's better to watch one video of 50 minutes in detail rather than 5 to 6 videos of each 20 mins.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much ❤️😇🙏
@varunsheth1003
@varunsheth1003 Жыл бұрын
Nice Explanation
@shivamnirmalkar6251
@shivamnirmalkar6251 2 ай бұрын
//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
@i6757
@i6757 2 ай бұрын
finding subarray and storing by brute force is O(n^3) how you are saying it is of O(n^2) @3:30 ?
@Mohit-fe5hx
@Mohit-fe5hx 8 ай бұрын
Next APJ Abdul Kalam Sir
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
🙏🙏❤️❤️ Although I can never become like him The Legend. But I will definitely try my best to contribute to the coding community 🙏❤️😇
@thestrangers6433
@thestrangers6433 3 ай бұрын
Best on youtube ❤
@dhruvaseri2082
@dhruvaseri2082 Ай бұрын
great work...
@TejalKumawat-hu9yi
@TejalKumawat-hu9yi 5 ай бұрын
best explaination on the internet for this question
@codestorywithMIK
@codestorywithMIK 5 ай бұрын
Thank you ❤️
@Kings-ik9lh
@Kings-ik9lh 8 ай бұрын
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);
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
This is the only best explanation I found for this problem. You are too good. Let's support this Legend and make him famous.
@alizhadigerov9599
@alizhadigerov9599 8 ай бұрын
in 29:54 you are putting the value 3 inside the stack, but after that you are putting indices. Do we need to put indices into the stack or values?
@this_is_ur_ms
@this_is_ur_ms 6 ай бұрын
Great Explanation, cleared everything. Thankyou.
@stroker01
@stroker01 3 ай бұрын
best!!!!!!!!!!!!
@eng.gaming5557
@eng.gaming5557 8 ай бұрын
Perfection belongs to one and only Mazhar Khan Sir... Thanks for ur efforts
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
❤️❤️🙏🙏means a lot
@stepup7052
@stepup7052 Жыл бұрын
thanks bhaia
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Glad i could help ❤️😇
@hiteshkumaryadav8373
@hiteshkumaryadav8373 8 ай бұрын
Wo duplicate wala to clear kiya hi nhi bhai, bas seedha bta diya ki ek jagah equal to lga diya. Wo to maine bhi kr liya tha bhai 🙂
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Apologies if you felt so. Take this for understanding - [17 , 55 , 82 , 55] Let me explain with an example. First, write all the subarrays in which each element nums[i] is minimum. [17 , 55 , 82 , 55] Let's write for 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] Let's write for 55: [55], [55, 82], [55, 82, 55] -----> Keep an eye on this Let's write for 82 : [82] Let's write for 55 : [55], [82, 55], [55, 82, 55] ---> NOTE here [55, 82, 55] is duplicate when we wrote for 55 above. To avoid the duplicate I mentioned above. In one of the calculations, NSR or NSL, don't put >= So, for last 55, we will get [55], [82, 55] Now, start calculating : For 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] - 4 subarrays in which 17 is minimum so, result = 4*17 For 55: [55], [55, 82], [55, 82, 55] - 3 subarrays in which 55 is minimum result += 3*55 For 82 : [82] - 1 subarray in which 82 is minimum result += 1*82 For last 55 [55], [82, 55] - 2 unique subarrays in which this 55 is minimum result += 2*55 Total = 68 + 165 + 82 + 165 = 425 Hope this helps everyone. Thank you ❣
@hiteshkumaryadav8373
@hiteshkumaryadav8373 8 ай бұрын
@@codestorywithMIK thank you for the explanation
@closer9689
@closer9689 Ай бұрын
💯🙏 wow!!
@adityatomar9820
@adityatomar9820 7 ай бұрын
god level explanation bhaii...i was struggling so much in this ques
@jayashankarmaddipoti6964
@jayashankarmaddipoti6964 8 ай бұрын
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.
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Definitely 💪💪💪 Keep going 🔥
@souravsingh9993
@souravsingh9993 Жыл бұрын
Thanks bro.
@Siddharth1924
@Siddharth1924 4 ай бұрын
top notch!!
@jagadeeshp1163
@jagadeeshp1163 8 ай бұрын
[71,55,82,55] why is this case giving me 703 instead of 503 def nsel(self,arr): n=len(arr) st=[] NSL=[-1 for i in range(n)] st=[0] for i in range(1,n): if arr[i]>arr[st[-1]]: NSL[i]=st[-1] st.append(i) else: while(st and arr[st[-1]]>=arr[i]): st.pop() if st: NSL[i]=st[-1] st.append(i) return NSL def nser(self,arr): n=len(arr) st=[] NSR=[n for i in range(n)] st=[n-1] for i in range(n-2,-1,-1): if arr[i]>=arr[st[-1]]: NSR[i]=st[-1] st.append(i) else: while(st and arr[st[-1]]>=arr[i]): st.pop() if st: NSR[i]=st[-1] st.append(i) return NSR def sumSubarrayMins(self, arr: List[int]) -> int: left=self.nsel(arr) right=self.nser(arr) cnt=0 for i in range(len(arr)): l=i-left[i] r=right[i]-i cnt+=(l)*(r)*arr[i] return cnt can some helpme out figuring the mistake i did ??
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
The correct answer is 425. Let me explain how. First, write all the subarrays in which each element nums[i] is minimum. [17 , 55 , 82 , 55] Let's write for 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] Let's write for 55: [55], [55, 82], [55, 82, 55] -----> Keep an eye on this Let's write for 82 : [82] Let's write for 55 : [55], [82, 55], [55, 82, 55] ---> NOTE here [55, 82, 55] is duplicate when we wrote for 55 above. To avoid the duplicate I mentioned above. In one of the calculations, NSR or NSL, don't put >= So, for last 55, we will get [55], [82, 55] Now, start calculating : For 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] - 4 subarrays in which 17 is minimum so, result = 4*17 For 55: [55], [55, 82], [55, 82, 55] - 3 subarrays in which 55 is minimum result += 3*55 For 82 : [82] - 1 subarray in which 82 is minimum result += 1*82 For last 55 [55], [82, 55] - 2 unique subarrays in which this 55 is minimum result += 2*55 Total = 68 + 165 + 82 + 165 = 425 Hope this helps everyone. Thank you ❣
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
This is one of the most critical corner case of this problem. Either in NSR or in NSL, replace >= check with > Any one of them.
@jagadeeshp1163
@jagadeeshp1163 8 ай бұрын
@@codestorywithMIK This question has lot of taught process needed to learn many things from this single question thanks for the tips thanks for the top level intuition behind this and thanks alot for the quick response
@RV-qf1iz
@RV-qf1iz Жыл бұрын
thanks bro i was able to solve this problem on my own after understanding the logic.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Glad to hear that 😇❤️🙏
@harshtiwari416
@harshtiwari416 8 ай бұрын
bhai zara aap brute force ki time complexity kaise n^3 hogyi explian kr skte hai????
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
My bad, a better Brute force also exists O(n^2) A better Brute Force : class Solution { public: int M = 1e9 + 7; int sumSubarrayMins(std::vector& arr) { int n = arr.size(); long result = 0; for (int i = 0; i < n; i++) { int minVal = arr[i]; for (int j = i; j < n; j++) { minVal = min(minVal, arr[j]); result = (result + minVal) % M; } } return result; } }; The time complexity of this brute-force approach is O(N^2) . In the worst case, you have to iterate through all N elements for both starting and ending indices.
@harshtiwari416
@harshtiwari416 8 ай бұрын
Bhai aap keh rahe ho ki duplicate add hojayenge but agar test case [17 , 55 , 82 , 55] isko bina >= ke kru toh sum kum aarha hai 483 na ki zyada sahi answer se 593 kindly explain
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
The correct answer is 425. Let me explain how. First, write all the subarrays in which each element nums[i] is minimum. [17 , 55 , 82 , 55] Let's write for 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] Let's write for 55: [55], [55, 82], [55, 82, 55] -----> Keep an eye on this Let's write for 82 : [82] Let's write for 55 : [55], [82, 55], [55, 82, 55] ---> NOTE here [55, 82, 55] is duplicate when we wrote for 55 above. To avoid the duplicate I mentioned above. In one of the calculations, NSR or NSL, don't put >= So, for last 55, we will get [55], [82, 55] Now, start calculating : For 17 : [17], [17, 55], [17, 55, 82], [17, 55, 82, 55] - 4 subarrays in which 17 is minimum so, result = 4*17 For 55: [55], [55, 82], [55, 82, 55] - 3 subarrays in which 55 is minimum result += 3*55 For 82 : [82] - 1 subarray in which 82 is minimum result += 1*82 For last 55 [55], [82, 55] - 2 unique subarrays in which this 55 is minimum result += 2*55 Total = 68 + 165 + 82 + 165 = 425 Hope this helps everyone. Thank you ❣
@sabbulingineni
@sabbulingineni 8 ай бұрын
Great video ❤ Pls do videos on solid principles and design patterns pls 🙏🙏🙏
@aayushranjan5572
@aayushranjan5572 11 ай бұрын
bhai pls brute force wale sb ka v code bata do
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
A better Brute Force : class Solution { public: int M = 1e9 + 7; int sumSubarrayMins(std::vector& arr) { int n = arr.size(); long result = 0; for (int i = 0; i < n; i++) { int minVal = arr[i]; for (int j = i; j < n; j++) { minVal = min(minVal, arr[j]); result = (result + minVal) % M; } } return result; } }; The time complexity of this brute-force approach is O(N^2) . In the worst case, you have to iterate through all N elements for both starting and ending indices.
@akworld2739
@akworld2739 5 ай бұрын
bhai plese mera sirf duplicate wala concept clear nhi hua
@gocrazy6177
@gocrazy6177 11 ай бұрын
How can someone be so good at explaning logics ??
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
🙏🙏🙏❤️❤️❤️
@gocrazy6177
@gocrazy6177 11 ай бұрын
@@codestorywithMIK 🙏🙏🙏 bless me sir
@k-CE-OmkarPathak
@k-CE-OmkarPathak 8 ай бұрын
op
@shubhamkewat2496
@shubhamkewat2496 Жыл бұрын
I Appreciate it.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot ❤️
@Abhay14
@Abhay14 8 ай бұрын
thanks a lot bhaiya , sayad hi aapse acchha koi iss question ko samjha skta tha this topic was so deep and interested
@tusharyadav5874
@tusharyadav5874 8 ай бұрын
Great explanation bhai :)
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
Thanks 🙂
@aadarshdontul5732
@aadarshdontul5732 3 ай бұрын
Intuition.... is the main thing that way you have explained.
@SurajKumar-ku1lg
@SurajKumar-ku1lg 4 ай бұрын
best video explanation of that problem
@DR-mq1le
@DR-mq1le Жыл бұрын
great explanation! thanks
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Glad it was helpful! 😇❤️
@sjxsubham...
@sjxsubham... 8 ай бұрын
best.... solution... thank MIK sir
@sohamjoshi25
@sohamjoshi25 8 ай бұрын
Great Video!!!! Keep it up.
@codestorywithMIK
@codestorywithMIK 8 ай бұрын
🙏🙏❤️❤️
L9. Sum of Subarray Minimum | Stack and Queue Playlist
23:35
take U forward
Рет қаралды 38 М.
Остановили аттракцион из-за дочки!
00:42
Victoria Portfolio
Рет қаралды 3,9 МЛН
The Joker wanted to stand at the front, but unexpectedly was beaten up by Officer Rabbit
00:12
哈莉奎因怎么变骷髅了#小丑 #shorts
00:19
好人小丑
Рет қаралды 54 МЛН
Learn JavaScript - Full Course for Beginners
3:26:43
freeCodeCamp.org
Рет қаралды 17 МЛН
Sum of Subarray Minimums - Leetcode 907 - Python
18:51
NeetCodeIO
Рет қаралды 33 М.
Ep : 5 I Jain Philosophy: An Introduction I Dr Vikas Divyakirti
3:29:27
Vikas Divyakirti
Рет қаралды 9 МЛН
Quiet Night: Deep Sleep Music with Black Screen - Fall Asleep with Ambient Music
3:05:46
C Programming Tutorial for Beginners
3:46:13
freeCodeCamp.org
Рет қаралды 12 МЛН
LeetCode 2104. Sum of Subarray Ranges
24:37
Kacy Codes
Рет қаралды 10 М.