Given question-
Let us build a stack data structure with the help of inbuilt vector class.
class myStack
{
vector < int > stack;
public:
int top();
void pop();
void push(int x);
}
Imagine the vector as a stack. The element at last position of the vector will be treated as top of the stack and element at stack[0] will be treated as the bottom of the stack.
Choose the correct option :
-
top() and pop() can be implemented to work in O(1) time complexity.
-
top() and push() can be implemented to work in O(1) time complexity.
-
either pop() or push() will work in O(N) and the other will work in O(1).
-
All functions can be implemented to work in O(1) time complexity.
I don’t understand how pop() will be implemented using vectors?