Re: [algogeeks] Re: Find push/pop/min or max in O(1)

2010-09-30 Thread Amit Mittal
we can use another stack to store the min/max number in stead of using pointers in every item. At the time of push, we have to do normal push in the first stack and in the extra stack, we can check the top value, if it's greater than the current item, we should push the current item in the extra st

[algogeeks] Re: Find push/pop/min or max in O(1)

2010-09-30 Thread Gene
Saurabh Singh's method is fine. It's even simpler to use a regular stack of ints, but re-implement push(x) and pop() to do 3 operations each. void mm_push(stack s, int x) { int min = mm_min(s); int max = mm_max(s); push(s, x); push(s, x < min ? x : min); push(s, x > max ? x : max); } i