Min Stack Leetcode | Get Min at pop | solution stack Hindi Explained | Data structure & Algorithms

Sdílet
Vložit
  • čas přidán 27. 02. 2022
  • This is the video under the series of DATA STRUCTURE & ALGORITHM in a STACK Playlist. Now we are going to solve a stack problem Min Stack or Get min at pop from GeeksForGeeks.
    Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
    complete DSA preparation: github.com/Prince-1501/Comple...
    ----------------------------------------------------------------------------------------
    ► 155. Min Stack
    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
    Implement the MinStack class:
    MinStack() initializes the stack object.
    void push(int val) pushes the element val onto the stack.
    void pop() removes the element on the top of the stack.
    int top() gets the top element of the stack.
    int getMin() retrieves the minimum element in the stack.
    Input
    ["MinStack","push","push","push","getMin","pop","top","getMin"]
    [[],[-2],[0],[-3],[],[],[],[]]
    Output
    [null,null,null,null,-3,null,0,-2]
    Explanation
    MinStack minStack = new MinStack();
    minStack.push(-2);
    minStack.push(0);
    minStack.push(-3);
    minStack.getMin(); // return -3
    minStack.pop();
    minStack.top(); // return 0
    minStack.getMin(); // return -2
    ►Min Stack: leetcode.com/problems/min-stack/
    ►Get min at pop: practice.geeksforgeeks.org/pr...
    ► Code In this video: github.com/Prince-1501/Hello_...
    ----------------------------------------------------------------------------------------
    *Follow me *
    LinkedIn► / iamprince
    Facebook► / helloworldofficials
    Instagram► / helloworldbyprince
    Twitter► / prince_king_
    Telegram► telegram.me/helloworldbyprince
    ----------------------------------------------------------------------------------------
    ►Our Playlists on:-
    ► Tree: • Tree Data Structure & ...
    ► Stack: • Stack & Queue Data Str...
    ► Hashing: • Hashing Data Structure...
    ► Graph: • Graph Data Structure &...
    ► Matrix: • Matrix (Multidimension...
    ► STL: • Standard Template Libr...
    ► Leetcode: • LeetCode Solutions And...
    ►Competitive Programming: • Full course in Competi...
    ►C++ Full Course : • C++ full Course in HINDI
    ►Algorithms: • L-01 || Prefix Sum Arr...
    ►Data Structure: • Data Structures with C...
    ------------------------------------------------------------------------
    🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
    ✨ Tags ✨
    leetcode problems
    leetcode problems java
    leetcode problems python
    leetcode problems that got me tired
    leetcode problems c++
    leetcode problems and solutions python
    leetcode problems playlist
    leetcode problems and solutions java
    leetcode problems in Hindi
    leetcode problems javascript
    leetcode problems and solutions
    leetcode problems of the day
    leetcode problems for beginners
    leetcode problems easy
    leetcode problems js
    Introduction to the graph data structure
    stack practice problems
    stack practice problems gfg
    leetcode stack questions
    leetcode stack queue
    stack hello world
    MIn stack leetcode solution
    Get Min at pop gfg
    remove consecutive duplicates from a string
    question asked in Google
    off-campus placement
    number of closed islands
    Practice stack data structure
    Stack in a data structure in Hindi
    Stack Full playlist for Beginners
    algorithms
    graph
    data structure
    sorting algorithms
    algorithm analysis
    gate computer science preparation
    programming languages
    #stack #Leetcode #DSA

Komentáře • 153

  • @abhaykumargoyal3895
    @abhaykumargoyal3895 Před rokem +21

    Bohht achhe se explain kiya aapne
    Koi aise nhi krata
    Jo Aise hme btaye ke abhi ruko socho...
    And apki language 👏🏻 ittni beginner friendly 🙏🏻🙏🏻
    Aapki speed, video quality, sound quality, apke explain krne ka way 🔥🔥just splendid👌🏻👌🏻

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem +3

      Thanks a ton Abhay, thanks for supporting me
      Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

    • @swapnilpatel9831
      @swapnilpatel9831 Před 11 měsíci

      do this without using extra space@@HelloWorldbyprince

  • @vaishalisharma5759
    @vaishalisharma5759 Před rokem +1

    the way you explain the question as well as the solution is really amazing. love to see your videos.☺

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

    Dil jeet liya bhaiya thank you from bottom of my heart.

  • @rohankadam7376
    @rohankadam7376 Před rokem

    Top notch explanation bhaiya !!! Thank you

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

    Your explanation was excellent. I had been stuck on this code for the past two days and watched numerous videos, but you covered everything in detail, including small points like void and int.😊😊Thank you so much.

  • @AdityaRajSingh-xy6gt
    @AdityaRajSingh-xy6gt Před rokem

    the explainations of the problem was very good bring more of these explanation on DSA related topics series.

  • @xboy2374
    @xboy2374 Před 10 měsíci +1

    samaj aa gyapura thank sir ji

  • @dennisjoshua8885
    @dennisjoshua8885 Před rokem

    superb bro, thank you.

  • @SwetaYadav-ir3lk
    @SwetaYadav-ir3lk Před rokem

    Really appreciated your effort bhaiya ❤ even a noob can understand very well from your video bhaiya 😊

  • @vitaminprotein2217
    @vitaminprotein2217 Před rokem +6

    Glad you share this point at 12:30 as while doing Valid Parentheses i did the same mistake by reversing the order in condition ,later i realised that this actually make difference lol

  • @SaumyaSharma007
    @SaumyaSharma007 Před 10 měsíci +1

    Legendary explanation. Thank u bhaiya.

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

    i got this in first attempt because you said to attempt , it really helped , i have completed this question without any help .

  • @DeepakKumar-oz5ky
    @DeepakKumar-oz5ky Před 2 měsíci

    great video bhiaya

  • @ritwikchawla9000
    @ritwikchawla9000 Před 2 lety +3

    The way you tell these small important things is really helpful. Really nice explanation !!!!

  • @harshitasingh4690
    @harshitasingh4690 Před 14 dny

    Great explanation👍👌

  • @ankitgaur7428
    @ankitgaur7428 Před 4 měsíci

    very good explanation

  • @jigyasupant453
    @jigyasupant453 Před 2 lety

    Thanks a lot bhaiya! Very helpful

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

    we have to make second another stack upto this I realised but how to maintain that to get a minimum element that I did not get from my thought side so upto half solution I reached

  • @rahulsati5819
    @rahulsati5819 Před 2 lety +2

    hn adha tak soch liya tha maine mja aagya bhaiyaji

  • @abhijitbiradar
    @abhijitbiradar Před rokem

    Great explanation. May Thnaks

  • @madhavdwivedi5146
    @madhavdwivedi5146 Před rokem

    maine doubly LL banayi thi minimum elements ki and jab pop krha tha tab agar top element mid node ke barabar tha to min pointer ko shift krdiya

  • @krishnasingh2520
    @krishnasingh2520 Před rokem +3

    Can someone explain🚩🚩📗 When I'm writing the same code in JAVA, it's not working. But When I'm changing the pop() method to:
    public void pop() {
    if(st.peek() == getMin()) min_st.pop(); // *Using getMin() instead of min_st.peek() works* ???

    st.pop();
    }
    //Please explain why writing min_st.peek() doesn't work?

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

      same problem bhai.Agar aapko solution smjh aaya iska to btana meko..

  • @gopinathbarat4633
    @gopinathbarat4633 Před rokem

    Well explained!

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      Thanks a lot buddy please share this video so that others can also get benefits from this

  • @Caroline-up8qw
    @Caroline-up8qw Před rokem

    Best and easy explanation...

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

    Great explanation sir

  • @pavanjain2178
    @pavanjain2178 Před 9 měsíci +1

    You explained it amazingly brother, but now this question has become a medium level question in leetcode and many more testcases are added where they are also testing multiple occurrences of possible min element. So an upgraded solution with same logic with a added frequency hashmap will look like this
    class MinStack:
    def __init__(self):
    self.stack = []
    self.min_stack = []
    self.min_freq_map = {}
    def push(self, val: int) -> None:
    self.stack.append(val)
    if self.min_stack:
    if val None:
    x = self.stack.pop()
    if self.min_stack[-1] == x:
    if self.min_freq_map[x] > 1:
    self.min_freq_map[x] -= 1
    else:
    self.min_freq_map.pop(x)
    self.min_stack.pop()

    def top(self) -> int:
    return self.stack[-1]
    def getMin(self) -> int:
    return self.min_stack[-1]
    leetcode.com/problems/min-stack/solutions/4137761/python-stack-hashmap-solution/

  • @shreyagrawal3806
    @shreyagrawal3806 Před rokem

    nice explaination

  • @shivanigangwar1192
    @shivanigangwar1192 Před rokem +1

    bhaiyaaaaaaaa bhot ache se ye bata diya apne.... thank youuuuuuuuuuuuuuuuuuuuuuuuuuuu

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

    Thank u sir 🙌🏻

  • @TEJASSADADE
    @TEJASSADADE Před rokem

    simple and clean

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

    well explained !!

  • @prabhatmishra5667
    @prabhatmishra5667 Před 2 lety +1

    Amazing content

  • @masumali8356
    @masumali8356 Před rokem

    you are the best.......masum.

  • @durgeshkushwaha2534
    @durgeshkushwaha2534 Před rokem +1

    Really very good logic and explanation ❤❤

  • @none2868
    @none2868 Před rokem

    Awesome explanation

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

    Great Video

  • @Fe-ironman
    @Fe-ironman Před 2 měsíci

    8:22 what if we popped 18 then 18 will pop out of stack but will remain in min_stack and then after that we pop 15 then that 15 will get removed from both stack and min_stack and now if we ask for min_element then now in min_stack there is 18 reamining but it was already popped out of main stack so how does this work?
    or what if we pop 15 then pop 18 then ask for get_Minvalue then what min value will be retuerend?? since min_Stack is empty?
    Am i being dumb or what i wrote do makes sense?
    how to fix this?? i think to fix this the min_stack should be as same size as the main_stack so that min_stack(as in use sorted version of stack in descending order) is empty case won't occur and when you pop from main_stack it should be popped from min_stack as well i guess. but this can never be O(1)

  • @OmprakashKumar-px2fb
    @OmprakashKumar-px2fb Před 2 lety

    really very helpful video for me

  • @LAXMI_SAHU
    @LAXMI_SAHU Před 2 měsíci

    TQ very much bhaiya ❤, before this video i have wasted my 2 hours , but after this video i cracked the answer.😊😊

  • @SurajRaj-zr5qu
    @SurajRaj-zr5qu Před 2 lety

    great vid 👍

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety

      thanks suraj
      Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @bazeer1498
    @bazeer1498 Před 11 měsíci

    Nice Explaination Bhai

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

    👍

  • @AdityaGupta-wc9nx
    @AdityaGupta-wc9nx Před 5 měsíci

    I think Map approach won't work when we have duplicate elements pushed

  • @luckilygamer5462
    @luckilygamer5462 Před 2 lety +1

    bhaiya badi badhiya smjhaye ek baar m dimag m dhuk gya.Thank you bhaiya

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety +1

      😂😂😂 thanks yaar
      Please, if possible then share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

    • @luckilygamer5462
      @luckilygamer5462 Před 2 lety

      @@HelloWorldbyprince theek h bhaiya 😊 bs aap issi trh se amazing content bnayee .

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

    Nice explaination

  • @naikajsevak6090
    @naikajsevak6090 Před rokem +1

    i have maintain double ended queue and time complexity of getting min element is O(1)

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem +1

      Nice way to solve this question
      Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @gajananambekar3194
    @gajananambekar3194 Před rokem

    Helpful

  • @kallabhalu6006
    @kallabhalu6006 Před 10 měsíci

    this solution uis showing emty stack expection in the getMin method

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

      yaar pls check my code once, if u still face this issue, then let me know

  • @aryangupta904
    @aryangupta904 Před 10 dny

    but ye bhi O(N) ni hogya space mei as hum ek aur stack banare

  • @KARANKUMAR-vp3nq
    @KARANKUMAR-vp3nq Před rokem

    great vid sir

  • @amulop
    @amulop Před 2 lety +1

    🔥🔥🔥 please cover aditya verma remaining questions also

  • @advitkothari7302
    @advitkothari7302 Před rokem

    gfg pr test case pass he nhi hue same code dala tha

  • @sourav6321
    @sourav6321 Před 2 lety +1

    Bhaiya aap pehle jo map pe implement kiye uspe to multiple same values mein dikkat nahi hoga? like 1 3 3 2 1 aise mei?

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety +1

      okayyyyy
      let's do one thing
      just take an dummy example which was on your mind
      and see that your exmplae is valid according to the question then
      follow my solutions

    • @sourav6321
      @sourav6321 Před 2 lety

      @@HelloWorldbyprince Samajh gaya bhaiya 😎

  • @ANKITSINGH-xv2et
    @ANKITSINGH-xv2et Před 2 lety

    great

  • @Niteshkumar-gw7cw
    @Niteshkumar-gw7cw Před 2 lety

    👌❤️

  • @kewtomrao
    @kewtomrao Před 2 lety +2

    i maintained a minHeap instead of minStack but that was (ologn) for pop :(
    What abt O(1) space approach?

    • @supriyamanna715
      @supriyamanna715 Před rokem

      not possible sir!

    • @psinity
      @psinity Před rokem

      @@supriyamanna715 it is possible to have o(1) if instead of pushing the value use push 2*val - minval

    • @psinity
      @psinity Před rokem

      ye o(1) space complexity ka nhi hai solution, jisko O(1) space complexity and O(1) time complexity ka chaiye ye rha
      class MinStack {
      private:
      std::stack stack;
      int min_value;
      public:
      MinStack() : min_value(INT_MAX) {}
      void push(int val) {
      if (stack.empty()) {
      stack.push(val);
      min_value = val;
      } else {
      if (val < min_value) {
      stack.push(2 * val - min_value);
      min_value = val;
      } else {
      stack.push(val);
      }
      }
      }
      void pop() {
      if (stack.empty()) {
      return;
      }
      int val = stack.top();
      stack.pop();
      if (val < min_value) {
      min_value = 2 * min_value - val;
      }
      }
      int top() {
      if (stack.empty()) {
      return INT_MAX;
      }
      int val = stack.top();
      if (val < min_value) {
      return min_value;
      } else {
      return val;
      }
      }
      int getMin() {
      return min_value;
      }
      };

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

      @@psinity 2 * val - min_value isse stack ki original value change jorahi pr push krte vkt. i do not know why we using this regardless of this???

  • @gauravbisht504
    @gauravbisht504 Před rokem

    Pop krne m agr minSt empty hua to

  • @omenn4553
    @omenn4553 Před 11 měsíci

    hashmap socha tha counter maintain karke par fir min find karne k liye loop chalana pada

  • @RitikKumar-bk6pj
    @RitikKumar-bk6pj Před rokem

    Bhaiya aapne video bnana bnd krdi hai kya?

  • @muneebwaqas400
    @muneebwaqas400 Před rokem

    I have used a very brute force solution .Like O(N) and why did'nt I was not able to think about it.FF to me

  • @apurvtewari3779
    @apurvtewari3779 Před 2 lety

    I made a pair {val,min} and updated min

  • @Sunil-lf1wd
    @Sunil-lf1wd Před rokem

    bhai yr frk itna hai mena min variable return kr diya but pne pure corner case check kiya
    koy nehi its my bad baki question mast hai

  • @sourabhsoni148
    @sourabhsoni148 Před rokem

    I though using a binary try and always storing the root node as the minimum element , its a bit over engineered i think ; p

  • @rambhakt06
    @rambhakt06 Před rokem

    i tried same approach but it does not work in java

  • @antaryamisingh6645
    @antaryamisingh6645 Před 2 lety +1

    Bhaiya user se input kaise lenge

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety +1

      Yaar wo leetcode ka platform khud kar raha hai ...
      I think bro u should watch me leetcode playlist ...first ...uske 3-4 questions starting ke dekh jao ..sab samjh me aa jayega

  • @KARANKUMAR-vp3nq
    @KARANKUMAR-vp3nq Před rokem

    nice

  • @1anuragmishra01
    @1anuragmishra01 Před rokem +2

    Best

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

    I think about priority_queue

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

    बकायदा चेक नहीं कर सकते। बकाएगा किया होता हैं, हा?

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

    bhai dsa complete krne me avg ketna time lgta he

  • @iftekhar_ansari
    @iftekhar_ansari Před rokem

    But what about duplicate elements bhaiyaa

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem +1

      usse kya hi farak padd jayega bro, aap ek baar khud se dry run kariye pen and paper pe aako clear ho jayega

  • @advitkothari7302
    @advitkothari7302 Před rokem

    kisi ne try kiya kya gfg pr nhi chl rha

  • @gamingworld2952
    @gamingworld2952 Před 2 lety

    op

  • @krushnapatil9196
    @krushnapatil9196 Před 2 lety +1

    background black kaise kiya?

  • @jivanninawe3190
    @jivanninawe3190 Před 2 lety +1

    Bhahiya thode aur question karo stack pr 😁😁

  • @mayank_singh_43
    @mayank_singh_43 Před rokem

    main araay bna k kar rha tha

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

    pair se socha tha

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

    My Approach :
    stack stack;
    map mpp;
    MinStack() {
    int c=0;
    }

    void push(int val) {
    stack.push(val);
    mpp[val]++;
    }

    void pop() {
    mpp[stack.top()]--;
    if(mpp[stack.top()]==0)
    mpp.erase(stack.top());
    stack.pop();
    }

    int top() {
    return stack.top();
    }

    int getMin() {
    return mpp.begin()->first;
    }
    };

  • @pritishpattnaik4674
    @pritishpattnaik4674 Před 2 lety

    I also took 2 stacks but got stuck

  • @psinity
    @psinity Před rokem

    ye o(1) space complexity ka nhi hai solution, jisko O(1) space complexity and O(1) time complexity ka chaiye ye rha
    class MinStack {
    private:
    std::stack stack;
    int min_value;
    public:
    MinStack() : min_value(INT_MAX) {}
    void push(int val) {
    if (stack.empty()) {
    stack.push(val);
    min_value = val;
    } else {
    if (val < min_value) {
    stack.push(2 * val - min_value);
    min_value = val;
    } else {
    stack.push(val);
    }
    }
    }
    void pop() {
    if (stack.empty()) {
    return;
    }
    int val = stack.top();
    stack.pop();
    if (val < min_value) {
    min_value = 2 * min_value - val;
    }
    }
    int top() {
    if (stack.empty()) {
    return INT_MAX;
    }
    int val = stack.top();
    if (val < min_value) {
    return min_value;
    } else {
    return val;
    }
    }
    int getMin() {
    return min_value;
    }
    };

  • @xboy2374
    @xboy2374 Před 10 měsíci

    bad approach
    // class MinStack {
    // public:
    // vectorv;
    // MinStack() { // constructor

    // }

    // void push(int val) {
    // v.push_back(val);
    // }

    // void pop() {
    // v.pop_back();
    // }

    // int top() {
    // return v[v.size()-1];
    // }

    // int getMin() {
    // int mn = v[0];
    // for(int i = 1;i

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

    medium level ka hogaya yeh sawal

  • @PankajYadav-nz4gn
    @PankajYadav-nz4gn Před rokem

    class MinStack {


    Stackst, s2; //here s2 is the minStack
    public MinStack() {

    }

    public void push(int val) {
    if( s2.isEmpty() || val

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      yesss

    • @prashantmishra9220
      @prashantmishra9220 Před rokem

      It maybe because you havent allocated memory to the stack i.e.
      Stack st=new Stack():
      'new' is the keyword which does memmory allocation in java...

    • @dungar5919
      @dungar5919 Před rokem

      Did you solve it ? If you have solved it pls can you share the code?

  • @sahilsingh5695
    @sahilsingh5695 Před rokem +1

    genuine feedback: please focus more on question rather than other commentary

  • @rohitnehra9519
    @rohitnehra9519 Před rokem

    This approach won't work for test case
    ["MinStack","push","push","push","push","pop","getMin","pop","getMin","pop","getMin"]
    [[],[512],[-1024],[-1024],[512],[],[],[],[],[],[]]