Stack using vector. Push fnction time complexity

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 :

Correct ans: All functions can be implemented to work in O(1) time complexity.

Doubt: How can push() function be implemented in O(1) time complexity, it will require O(N) complexity when it hits maximum capacity, right?

2 Likes