A tiny question about two stacks solution.


  • 0
    S

    Assume that we use two stacks solution, s1 for the global and s2 for the min.

    If all the number are all no smaller than 1, and the first element is 1, we need to push 1 into both stacks. Then we push hundreds of number that larger than 1 into s1. Now we can only get the first minimal number, that's 1. What about then? How can we get other small numbers in s1 in O(1)?


  • 0
    A

    Can you please explain your question with some example? Let me guess what you are asking:

    say, maxSize = 10

    push (-3)
    push (-5)
    push (-4)
    push (-2)
    push (1)
    push (2)
    push (5)
    push (10)
    push (12)
    push (50)

    Stacks would look like:

    S1:

    50
    12
    10
    5
    2
    1
    -2
    -4
    -5
    -3

    S2

    -5
    -5
    -5
    -5
    -5
    -5
    -5
    -5
    -5
    -3

    Please let me know if I am wrong.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.