L15. Stock Span Problem | Stack and Queue Playlist

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

КОМЕНТАРІ • 27

  • @aparnaverma1562
    @aparnaverma1562 4 місяці тому +7

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

  • @mrmehran879
    @mrmehran879 5 місяців тому +11

    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

  • @Himanshugupta-bw9ht
    @Himanshugupta-bw9ht 5 місяців тому +5

    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.

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

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

  • @iamnoob7593
    @iamnoob7593 13 днів тому

    Brilliant explanation , Thank you

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

    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 Місяць тому

    Great solution!

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 5 місяців тому +4

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

  • @showdownx8276
    @showdownx8276 5 місяців тому +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

  • @abhishek_1510
    @abhishek_1510 28 днів тому

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

  • @we_crood
    @we_crood 29 днів тому

    Able to solve Thanks :)

  • @studystuff51
    @studystuff51 4 місяці тому +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 2 місяці тому

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

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

    thanks bhaiya

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

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

  • @sanchitdeepsingh9663
    @sanchitdeepsingh9663 5 місяців тому

    thanks

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

    understood

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

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

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

      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.

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

  • @JohnSnow-r1e
    @JohnSnow-r1e 5 місяців тому +1

    First view and comment

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

    😁

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

    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

  • @nobody-kk8uh
    @nobody-kk8uh 5 місяців тому +1

    1st comment

  • @abhinjr4918
    @abhinjr4918 5 місяців тому

    Please pin me
    for 2 days❤

  • @maverick_8707
    @maverick_8707 5 місяців тому

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

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

    Thanks