Tuesday, April 12, 2011

Stack - getMin() in O(1)

Use two stacks, s1 and s2.
Push: Always push in s2, push in s1 too if value is less than top.
Pop:Pop from s2, if popped value is equal to top of s1, pop from s1 too.
Min: Top of s1.

1 comment:

Siva Eluri said...

AweSome !! Explanation dude.

Its working fine