• + 5 comments

    Hi, your idea is cool!

    +

    1. We can stack max elements only
    2. We can store count of elements that less than max and that have been pushed after max
    StackNode {
        int max
        int elementsAboveMax = 1
    }
    
    push {
        if (stack.peek.max < val)
        	stack.push( StackNode(val) )
        else
            stack.peek.elementsAboveMax++
    }
    
    pop {
        if (--stack.peek.elementsAboveMax == 0) stack.pop()
    }
    
    print {
        sout( stack.peek.max )
    }