907. Sum of Subarray Minimums | Monotonic Stack | Brute - Better - Optimal

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

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

  • @ARYANMITTAL
    @ARYANMITTAL  7 หลายเดือนก่อน +54

    Don't know what LC smoked, before placing this Problem as Medium 🫠🫠

  • @gdivya1895
    @gdivya1895 7 หลายเดือนก่อน +30

    This question ate my brain for the past two hours, I've tried all other videos available for this question, including big names like neetcode, but your solution is the only one that I could understand from start to finish. Well done sir. Earned a subscribe.

  • @madhubala5655
    @madhubala5655 3 หลายเดือนก่อน +5

    Finally after watching the solution in 4 different channels I understood it here

  • @dhushyanthankannan8856
    @dhushyanthankannan8856 หลายเดือนก่อน

    This is the greatest explanation among all the explanations available in yt

  • @jjjyotijain
    @jjjyotijain 5 หลายเดือนก่อน +3

    bro, what an amazing explanation.
    now I don't have any doubt left after watching this video.
    and now I can easily solve the problems of same pattern.
    Thank you very much for such a great explanation😍👏👏.

  • @mcw0805
    @mcw0805 7 หลายเดือนก่อน +2

    Thank you so much for explaining HOW the count works. I was trying to recall what I learned in my combinatorics class years ago lol

  • @tejaschalke1778
    @tejaschalke1778 7 หลายเดือนก่อน +13

    I was pissed I couldn't solve this problem but, now I feel a little better considering how hard this was. Also please make a video on KMP 😮‍💨, waiting from the previous weekly contest.

    • @ARYANMITTAL
      @ARYANMITTAL  7 หลายเดือนก่อน +5

      That video is out bro, that day only it came - Write on yt - Kmp by Aryan Mittal, you will get the video ❤️❤️🫂

    • @tejaschalke1778
      @tejaschalke1778 7 หลายเดือนก่อน

      @@ARYANMITTAL damn i completely missed it 😅.

  • @k.k.harjeeth5422
    @k.k.harjeeth5422 2 หลายเดือนก่อน

    24:06 this was brilliant !

  • @homeroserna9983
    @homeroserna9983 6 หลายเดือนก่อน

    Great solution. I like how you worked out the problem step by step completely before going to the code. You made a hard problem seem easy.

  • @gonuguntlakoushik899
    @gonuguntlakoushik899 7 หลายเดือนก่อน +3

    loved your way of explanation❤

  • @juliankuhn9742
    @juliankuhn9742 7 หลายเดือนก่อน +2

    thank you this is the best explanation I found :)

  • @ohhpeejoshi9110
    @ohhpeejoshi9110 2 หลายเดือนก่อน +1

    thankyou so much buddy! very nice

  • @k.satish3663
    @k.satish3663 7 หลายเดือนก่อน +2

    totally understood ! thank you so much bro

  • @physicstyco8631
    @physicstyco8631 หลายเดือนก่อน

    amazing explanation bro

  • @lohithaadapala6989
    @lohithaadapala6989 2 หลายเดือนก่อน

    Excellent!

  • @vaibhavagarwal1479
    @vaibhavagarwal1479 3 หลายเดือนก่อน

    I cant get a hold on why left*right works i get that it tells us that how many subarrays will include that particular number, but cant understand why it works?? anyone explain if possible, Great video by the way....Clear and precise.

  • @saurabhchaudhari4157
    @saurabhchaudhari4157 2 หลายเดือนก่อน +1

    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

  • @kannank4269
    @kannank4269 7 หลายเดือนก่อน +5

    How aryan can maintain work n leetcode parallely

  • @sagarsaini5377
    @sagarsaini5377 2 หลายเดือนก่อน

    finally a good explaination.

  • @mdsajidanwar6416
    @mdsajidanwar6416 7 หลายเดือนก่อน +4

    Given constraint is 3x10^4 , then O(n^2) should work then why it's TLE showing

  • @mrityunjoybarman9098
    @mrityunjoybarman9098 7 หลายเดือนก่อน

    Actually I'm not good at hard problems but after reading the problem I don't know how it clicked my mind and cracked the monotonic stack solution.

  • @josephsamuelm4608
    @josephsamuelm4608 7 หลายเดือนก่อน +1

    Bro can u solve Sum of Subarray Ranges as well.
    Thanks in advance !

  • @jaatharsh
    @jaatharsh 7 หลายเดือนก่อน

    superb explanation, really like it (tired of python explanation :D)

  • @aa_maruf
    @aa_maruf 7 หลายเดือนก่อน

    Brilliant explanation♥

  • @deepneurallearner
    @deepneurallearner 5 หลายเดือนก่อน

    Bhai aapne cnt ko tho badhaya hi nahi jab voh while loop main nahi ghus raha hai tab???

  • @codeman0017
    @codeman0017 7 หลายเดือนก่อน +2

    #include
    #include
    #include
    using namespace std;
    class Solution {
    public:
    int sumSubarrayMins(vector& arr) {
    stack st;
    vector next_lesser(arr.size(), arr.size());
    vector prev_lesser(arr.size(), -1);
    for (int i = 0; i < arr.size(); i++) {
    while (!st.empty() && arr[st.top()] >= arr[i]) {
    next_lesser[st.top()] = i;
    st.pop();
    }
    prev_lesser[i] = st.empty() ? -1 : st.top();
    st.push(i);
    }
    long long answer = 0;
    double mod = 1e9 + 7;
    for (int i = 0; i < arr.size(); i++) {
    long long left = i - prev_lesser[i];
    long long right = next_lesser[i] - i;
    answer += arr[i] * left * right;
    answer %= (long long)mod;
    }
    return (int)answer;
    }
    };
    int main() {
    Solution sol;
    vector arr = {3, 1, 2, 4};
    cout

  • @ishanbhardwaj6082
    @ishanbhardwaj6082 7 หลายเดือนก่อน

    Correct me if I'm wrong, but I believe the code fails for brute and better. Because it is not taking into account single element subarrays. Thank you for the stack solution tho, I was stuck on it the whole day lol

  • @YashGulhane-uv9yf
    @YashGulhane-uv9yf 16 วันที่ผ่านมา

    watched

  • @gauristar4094
    @gauristar4094 3 หลายเดือนก่อน

    thanksssss

  • @aryansonwani7061
    @aryansonwani7061 7 หลายเดือนก่อน +2

    did it by myself in half hour

  • @gaishurajput8239
    @gaishurajput8239 7 หลายเดือนก่อน +1

    Showing TLE

  • @AkashDeep-jp1ts
    @AkashDeep-jp1ts 4 หลายเดือนก่อน

    Fucked up my mind!

  • @samiranroyy1700
    @samiranroyy1700 23 วันที่ผ่านมา

    🧡❤❤❤❤❤❤❤❤❤

  • @abc-ym4zs
    @abc-ym4zs 7 หลายเดือนก่อน

    bhaiya to improve logical thinking should i need to do cp bhaiya i am in third year i am not getting interest any tips bhaiya

  • @aryansonwani7061
    @aryansonwani7061 7 หลายเดือนก่อน +1

    stack st;
    st.push(-1);
    int n=arr.size();
    vector dp(n);
    long long ans=0;
    for(int i=0;iarr[i-1])
    dp[i]=(arr[i]+dp[i-1]);
    else
    {
    // 2nd case
    while(st.top()!=-1 and arr[i]

    • @blackvelta1913
      @blackvelta1913 7 หลายเดือนก่อน

      bro can you explain it please

  • @veekykumar4211
    @veekykumar4211 5 หลายเดือนก่อน

    Why Wrong Answer
    Runtime: 7 ms
    Case 1
    Case 2
    Input
    arr =
    [11,81,94,43,3]
    Output
    384
    Expected
    444
    class Solution {
    public:
    int sumSubarrayMins(vector& arr) {
    int MOD = 1e9 + 7;
    int n = arr.size();
    vector left(n,0), right(n,0);
    stacksLeft, sRight;

    for(int i=0;iarr[i]){
    cnt+=sLeft.top().second;
    sLeft.pop();
    }
    sLeft.push({arr[i],cnt});
    left[i]=cnt;
    }
    for(int i=n-1;i>=0;i--){
    int cnt=1;
    if(!sRight.empty() && sRight.top().first>=arr[i]){
    cnt+=sRight.top().second;
    sRight.pop();
    }
    sRight.push({arr[i],cnt});
    right[i]=cnt;
    }
    long long sum = 0;
    for(int i=0;i