-4

I saw this answer to the question about designing a stack with findmin in O(1) time:

https://stackoverflow.com/a/3435998/2653179

What if the request is the same:

Devise a stack-like data structure that does push, pop and min (or max) operations 
in O(1) time. There are no space constraints.

But deletemin also should be in O(1)? Is it possible?

Community
  • 1
  • 1
user2653179
  • 393
  • 1
  • 6
  • 21

1 Answers1

5

No. It cannot be done - because it will allow O(n) sorting.

Assume there is such a DS, let it be D
Let A be an input array.
push all elements from A to D.
while D is not empty:
   yield D.min()
   D.popMin()

The above will run in O(n) assuming popMin() and min() are O(1), and will sort an array.
However, sorting is Omega(nlogn) problem. Contradiction.
Thus we can conclude such a DS does not exist,

amit
  • 175,853
  • 27
  • 231
  • 333