Stack operations

S

Stewart

Is it possible to implement a stack in which push, pop, and min all
work in O(1) or constant number of steps.

push inserts element at the top of the stack.
pop removes element at the top of the stack.
min returns the element with minimum value in the stack.

There are no restrictions, i.e any amount of resouces (memory, cpu,
etc) can be used for implementation. The only requirement is that all
three operations must be O(1).
 
A

Artie Gold

Stewart said:
Is it possible to implement a stack in which push, pop, and min all
work in O(1) or constant number of steps.

push inserts element at the top of the stack.
pop removes element at the top of the stack.
min returns the element with minimum value in the stack.

There are no restrictions, i.e any amount of resouces (memory, cpu,
etc) can be used for implementation. The only requirement is that all
three operations must be O(1).
Yes. Now what's your question about C++?

--ag
 
B

benben

Stewart said:
Is it possible to implement a stack in which push, pop, and min all
work in O(1) or constant number of steps.

push inserts element at the top of the stack.
pop removes element at the top of the stack.
min returns the element with minimum value in the stack.

There are no restrictions, i.e any amount of resouces (memory, cpu,
etc) can be used for implementation. The only requirement is that all
three operations must be O(1).

Yes! And believe it or not, it's easy! Hint: use the standard library.

ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top