L4. Implement Min Stack | Stack and Queue Playlist

Поділитися
Вставка
  • Опубліковано 14 лис 2024

КОМЕНТАРІ • 54

  • @devjindal8309
    @devjindal8309 4 місяці тому +40

    Dangerous question with mind blowing explanation ❤

  • @kamalesh4904
    @kamalesh4904 3 місяці тому +17

    For the optimal approach
    Use Long datatype for stack and min to pass edge cases as int may overflow when we multiply it by 2

    • @studystuff51
      @studystuff51 3 місяці тому

      need to use long long, and nice DP btw

    • @kamalesh4904
      @kamalesh4904 3 місяці тому

      @@studystuff51 thank you :)

  • @rudrajitgupta3688
    @rudrajitgupta3688 2 місяці тому +6

    Thank you striver for the intuition. I tried to implement the min stack using DLL where node represents (val,min,next,prev)
    all in O(1) optn
    push and pop represents addFirst remove first, and top will always hold min

    • @rudrajitgupta3688
      @rudrajitgupta3688 2 місяці тому +1

      class Data{
      int val;
      int min;
      Data next;
      Data prev;
      Data(int val, int min){
      this.val=val;
      this.min = min;
      this.prev=null;
      this.next=null;
      }
      }
      class MinStack {
      int min;
      Data head;
      Data tail;
      public MinStack() {
      this.min= Integer.MAX_VALUE;
      this.head = new Data(-1, min);
      this.tail = new Data(-1,min);
      this.head.next = this.tail;
      this.tail.prev = this.head;
      }
      public void push(int val) {
      // Data node = new Data(val, this.min);
      if(this.head.next == this.tail){
      min = val;
      Data node = new Data(val,min);
      add(node);
      return;
      }
      min = Math.min(val, this.head.next.min);//-2, -3
      Data node = new Data(val, min);
      add(node);
      }
      //(-3,-3),(0,-2),(-2,-2);
      //min = -3
      public void pop() {
      remove(this.head.next);
      //min = this.head.next.min;
      }
      public int top() {
      return this.head.next.val;
      }
      public int getMin() {
      return this.head.next.min;
      }
      public void add(Data node){
      Data next = this.head.next;// -1 ->20->-1
      node.next= next;
      next.prev=node;
      node.prev=this.head;
      this.head.next=node;
      }
      public void remove(Data node){
      //-1->10->20->-1
      Data next= node.next; //20
      this.head.next= next;
      next.prev = this.head;
      node.next=null;
      node.prev=null;
      }
      }
      /**
      * Your MinStack object will be instantiated and called as such:
      * MinStack obj = new MinStack();
      * obj.push(val);
      * obj.pop();
      * int param_3 = obj.top();
      * int param_4 = obj.getMin();
      */

  • @MohdYaqoob-s3k
    @MohdYaqoob-s3k Місяць тому +2

    took me 1 hour to understand this code. This is awesome. The equation is just 🔥🔥🔥🔥🔥🔥

  • @Dhruv-od3tc
    @Dhruv-od3tc 3 місяці тому +3

    for all those who failed at 28th test case
    this worked for me use long instead of int and while returing do typecast

  • @piyushmahale9024
    @piyushmahale9024 3 місяці тому +10

    30% Completed till now ✅✅

  • @IamNikhil2121
    @IamNikhil2121 3 місяці тому +1

    timestamps
    0:00 - Question Overview
    1:45 - brute force solving Approach
    4:34 - brute force pseudocode
    6:50 - Brute force Time Complexity analysis
    7:15 - Optimal solving approach
    15:06 -Optimal pseudocode
    19:38 - Time Complexity of Optimal
    20:34 - Outro

  • @ritikshandilya7075
    @ritikshandilya7075 27 днів тому

    Thankyou so much Striver for great explanation

  • @ItsHarsh18
    @ItsHarsh18 7 днів тому

    watched for ig two or three times finally got it !!

  • @JunaidShareef-j4u
    @JunaidShareef-j4u 24 дні тому +2

    it has given medium level LC but i think it is harder to understand until striver comes here to understand us

  • @Rahul-jr4gf
    @Rahul-jr4gf 10 днів тому

    Thank you so much for amazing explanation

  • @theinsidestory8973
    @theinsidestory8973 3 місяці тому +1

    00:04 Design a minimum stack that includes get min operation
    02:41 Implementing Min Stack by tracking current minimum value while pushing elements.
    05:00 Implementing a min stack with push, pop, and getMin operations
    07:30 Implementing Min Stack using a simple stack and integer max as placeholder.
    09:57 Implementing Min Stack involves inserting modified values and updating the minimum.
    12:26 Modify minimum value in Min Stack implementation
    15:14 Designing a minimum stack by implementing push and pop operations
    17:44 Implementing Min Stack in a Stack structure
    19:58 Implementing Min Stack with optimized approach

  • @tilu391
    @tilu391 3 місяці тому +1

    we can just use a variable which stores minimum element ,before every push , we can compare and update accordingly and while popping if we are popping min element , then we can traverse to find the min element...

    • @kishoregowda8210
      @kishoregowda8210 2 місяці тому +1

      Since we are trying to achieve getMin in O(1), this approach would not work.

  • @apmotivationakashparmar722
    @apmotivationakashparmar722 2 місяці тому +1

    Thank you so much for this explaination. I am following your A-Z sheet . Can you please upload videos of left Question in A-Z sheet and Article as well .

  • @RajChaturvedi-s1r
    @RajChaturvedi-s1r 2 місяці тому +3

    at 4:35 if a interviewer ask us to design a stack we can design it using linked but after designing how we can use in this solution like out of class..

  • @oyeesharme
    @oyeesharme Місяць тому +1

    thank you bhaiya

  • @pBERA0_0
    @pBERA0_0 16 днів тому +1

    couldnt understand intuition behind 2*val-min at all.

  • @radhepatel6876
    @radhepatel6876 3 місяці тому +3

    For the Second method consider this code as the TUF website's code is generating wrong output for A edge test case
    class MinStack {
    Stack st = new Stack();
    Long min;
    public MinStack(){
    min = Long.MAX_VALUE;
    }

    public void push(int val) {
    Long value = Long.valueOf(val);
    if(st.isEmpty()){
    min = value;
    st.push(value);
    }
    else{
    if(value>min){
    st.push(value);
    }
    else{
    st.push(2*value-min);
    min=value;
    }
    }
    }

    public void pop() {
    if(st.isEmpty()){
    return;
    }
    Long val = st.pop();
    if(val

  • @jaydeeppatidar4189
    @jaydeeppatidar4189 Місяць тому +2

    When heap playlist will be launched on youtube?

  • @sahilmujawar8217
    @sahilmujawar8217 8 днів тому

    understood ❤

  • @prabhakaran5542
    @prabhakaran5542 3 місяці тому +1

    Understood ❤

  • @sauravfarkade7032
    @sauravfarkade7032 4 місяці тому +2

    Understood!!

  • @ushibuii8702
    @ushibuii8702 4 місяці тому

    wow got lucky, thanks for uploading

  • @namannema3349
    @namannema3349 Місяць тому

    op video and
    explanation

  • @KartikeyTT
    @KartikeyTT 3 місяці тому +1

    tysm sir

  • @SunnyKumar-dw9ze
    @SunnyKumar-dw9ze 2 місяці тому

    Why multiplication with 2

  • @sanketatmaram
    @sanketatmaram 2 місяці тому

    Great!

  • @DeadPoolx1712
    @DeadPoolx1712 Місяць тому

    UNDERSTOOD;

  • @AkashkumarYadav-r4b
    @AkashkumarYadav-r4b 2 місяці тому

    Nice

  • @SibiRanganathL
    @SibiRanganathL 3 місяці тому

    Understood

  • @Shivi32590
    @Shivi32590 3 місяці тому

    understood

  • @priyaanshusoni
    @priyaanshusoni 3 місяці тому

    His expressions at 12:56 😂😂😂😂

  • @roiyaruryuga5183
    @roiyaruryuga5183 3 місяці тому +3

    Hello Striver, We really want you to take rest. You have been working really hard lately.

  • @THOSHI-cn6hg
    @THOSHI-cn6hg 3 місяці тому +3

    bro looks tired

  • @hashcodez757
    @hashcodez757 4 місяці тому

    Thanks alot Bhaiya!!
    Understood

  • @prathameshjadhav2942
    @prathameshjadhav2942 3 місяці тому +4

    Very low energy #striver_79

  • @vinit_notfound
    @vinit_notfound 4 місяці тому

    ❤❤❤

  • @shiva.g1898
    @shiva.g1898 4 місяці тому

    👍👍

  • @varunsingh8322
    @varunsingh8322 4 місяці тому

    lesgoogogogoog

  • @crazymemes4080
    @crazymemes4080 3 місяці тому +1

    btw now i got it why striver bhaya is not providing code in the video :) so that we check the website too 😄 damn 6 star coder

  • @anikbiswas8
    @anikbiswas8 4 місяці тому

    only 22 testcases passed using second method

    • @Surya-teja2612
      @Surya-teja2612 3 місяці тому +1

      How about in first method

    • @worldfacts6760
      @worldfacts6760 3 місяці тому

      @@Surya-teja2612 all text case will pass

    • @yatharthgupta6468
      @yatharthgupta6468 2 місяці тому

      use ll

    • @yatharthgupta6468
      @yatharthgupta6468 2 місяці тому +2

      #define ll long long
      class MinStack {
      public:
      stack st;
      ll min;
      MinStack() {}
      void push(int val) {
      if(st.empty()) {
      st.push(val);
      min = val;
      } else {
      if(val < min) {
      st.push(2ll * val - min);
      min = val;
      } else {
      st.push(val);
      }
      }
      }
      void pop() {
      ll top = st.top();
      if(top < min) {
      min = 2ll * min - top;
      }
      st.pop();
      }
      int top() {
      ll top = st.top();
      if(top < min) {
      return min;
      } else {
      return top;
      }
      }
      int getMin() {
      return min;
      }
      };
      /**
      * Your MinStack object will be instantiated and called as such:
      * MinStack* obj = new MinStack();
      * obj->push(val);
      * obj->pop();
      * int param_3 = obj->top();
      * int param_4 = obj->getMin();
      */

  • @crazymemes4080
    @crazymemes4080 3 місяці тому

    understood sirrrr

  • @lokesh8660
    @lokesh8660 2 місяці тому

    Nice