Problem statement

https://binarysearch.com/problems/Virtually-Cloneable-Stacks/

Solution

What matters is number of elements in stacks, so keep lenghts of stacks in list.

Complexity

It is O(1) for time for all functions and O(n) for space.

Code

class VirtuallyCloneableStacks:
    def __init__(self):
        self.stack = [0]

    def copyPush(self, i):
        self.stack += [self.stack[i] + 1]

    def copyPop(self, i):
        self.stack += [self.stack[i] - 1]

    def size(self, i):
        return self.stack[i]