Please note that there is no limitation of memory.
I need to insert int from 1 to 1000.
I can do the each of the following operations in constant order of time:
- push():adds to the top
- pop():removes the top element
- getMax(): returns the max element
Please suggest me appropriate datastructure.
Since there is no limitation of memory, I will use 2 vectors – one for the actual data on the stack, and the other to keep track of the max at every state of the stack.
For simplicity sake I’m assuming this stack holds only +ve ints.
I know this doesn’t have any error checking. But I am just providing the data structure idea here, not the full-fledged solution.