L15. Stock Span Problem | Stack and Queue Playlist

Поделиться
HTML-код
  • Опубликовано: 4 фев 2025

Комментарии • 33

  • @aparnaverma1562
    @aparnaverma1562 5 месяцев назад +13

    I was able to come up with solution by myself.
    All thanks to you for making me improve my critical thinking

  • @mrmehran879
    @mrmehran879 6 месяцев назад +14

    class StockSpanner {
    public:
    int index;
    stack st;
    StockSpanner() {
    index =-1;
    // st.clear();
    }
    int next(int price) {
    index+=1;
    int ans;
    while(!st.empty() && st.top().first

  • @harshitsrivastava3154
    @harshitsrivastava3154 День назад

    I was able to solve the question myself thank you so much striver

  • @Himanshugupta-bw9ht
    @Himanshugupta-bw9ht 6 месяцев назад +6

    What a timing. I was solving this problem from SDE sheet and at first got disappointed as there was no link to this problem. I'm glad that I decided to checkout the channel. Unbeatable explanation like always. Thanks.

  • @kishoregowda8210
    @kishoregowda8210 Месяц назад +1

    I solved it by building a monotonic stack (executed in lesser time when compared to above optimal approach). It involves storing "Stack" type custom class which has "val" and "span" in Deque. The approach involves storing the Stack type classes in decreasing fashion. Whenever we encounter the value greater than stack.peek(), pop the elements and add it's corresponding span values. Below is my code for understanding.
    class StockSpanner {
    Deque stack;
    public StockSpanner() {
    stack = new ArrayDeque();
    }
    public int next(int price) {
    int span = 1;
    while (!stack.isEmpty() && stack.peek().val

  • @deepkodes4434
    @deepkodes4434 4 месяца назад +6

    liked it better when u gave the clear code at the end :)

  • @iamnoob7593
    @iamnoob7593 Месяц назад

    Brilliant explanation , Thank you

  • @KalingaAbhisek
    @KalingaAbhisek 4 месяца назад +2

    Java Code
    class StockSpanner {
    Stack st = new Stack();
    int ind;
    public StockSpanner() {
    st = new Stack();
    ind = -1;
    }
    public int next(int price) {
    ind++;
    while(!st.isEmpty() && st.peek()[1]

  • @kritangnasingh5934
    @kritangnasingh5934 2 месяца назад

    Great solution!

  • @showdownx8276
    @showdownx8276 6 месяцев назад +1

    Arre love you bhaiya ❤ i have done arrays, linked list, binary search, recursion ….. and was looking for stack n queue.
    What a timing bhaiya ji 🥹 mauj kara di

  • @we_crood
    @we_crood 2 месяца назад

    Able to solve Thanks :)

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 6 месяцев назад +4

    class StockSpanner {
    Stackst;
    public StockSpanner(){
    st=new Stack();
    }
    public int next(int price){
    int spam=1;
    while(!st.isEmpty()&&st.peek ()[0]

  • @abhinavabhi3568
    @abhinavabhi3568 Месяц назад

    Understood

  • @oyeshxrme
    @oyeshxrme 3 месяца назад

    thanks bhaiya

  • @sanchitdeepsingh9663
    @sanchitdeepsingh9663 6 месяцев назад

    thanks

  • @MJBZG
    @MJBZG 6 месяцев назад

    understood

  • @studystuff51
    @studystuff51 6 месяцев назад +3

    won't space complexity also be O(2N) as we are storing both value and index in stack? Also I tried to do the question by myself, and here is my solution:
    class StockSpanner {
    public:
    vector prices;
    stack st;
    StockSpanner() {
    index = -1;
    }

    int next(int price) {
    int ans;
    prices.push_back(price);
    while(!st.empty() && price >= prices[st.top()])
    st.pop();
    if(!st.empty()) ans = prices.size()-1-st.top();
    else ans = prices.size();
    st.push(prices.size()-1);
    return ans;
    }
    };

    • @Akash-Bisariya
      @Akash-Bisariya 3 месяца назад

      You are also using prices vector and stack here so space complexity will be O(2N) here also.

  • @abhishek_1510
    @abhishek_1510 2 месяца назад

    in 2nd approach space complexity should be O(2n) right ? (because we are using a stack of )

    • @sahilsukhdeve4695
      @sahilsukhdeve4695 11 дней назад +1

      no, 0(n) is for iterating on array & next o(n) for calculating the pge of each element. that results into overall tc of 0(2n)

  • @omkarshendge5438
    @omkarshendge5438 6 месяцев назад

    why do we need to reinitialize the index to -1 in the stockspanner() method guys? if we have declared it globally?

  • @subhasrisb11tha28
    @subhasrisb11tha28 4 месяца назад

    😁

  • @charuprabha8714
    @charuprabha8714 6 месяцев назад +1

  • @digantamondal2397
    @digantamondal2397 5 месяцев назад

    suppose my input is
    10 10 10 10
    Sample Output 1:
    1 1 1 1
    then how can you handle it

    • @praffulbisht8520
      @praffulbisht8520 5 месяцев назад

      I think sample output is wrong coz in condition it is stated that it will count for price less than or equal to current days.

  • @atharvajagtap1233
    @atharvajagtap1233 2 месяца назад

    Java Code
    class Pair {
    I index;
    V value;
    public Pair(I key, V value) {
    this.index = key;
    this.value = value;
    }
    public I getIndex() {
    return index;
    }
    public V getValue() {
    return value;
    }
    }
    public class StockSpanner {

    Stack st;
    int index;
    public StockSpanner() {
    this.st=new Stack();
    this.index=-1;
    }
    public int next(int price) {
    index++;
    while(!st.empty()&&st.peek().value

  • @JohnSnow-r1e
    @JohnSnow-r1e 6 месяцев назад +1

    First view and comment

  • @nobody-kk8uh
    @nobody-kk8uh 6 месяцев назад +1

    1st comment

  • @abhinjr4918
    @abhinjr4918 6 месяцев назад

    Please pin me
    for 2 days❤

  • @maverick_8707
    @maverick_8707 6 месяцев назад +1

    I'm waiting this for years ...thanks striver @takeUforward.... yesterday u said and u delivered

  • @fanofabdevillersandmathslo5960
    @fanofabdevillersandmathslo5960 15 дней назад +1

    Understood

  • @abhinanda7049
    @abhinanda7049 9 дней назад

    understood

  • @subee128
    @subee128 6 месяцев назад

    Thanks