0

I'm trying to understand the space complexity of moving elements from one stack to another stack.

I found this article on leetcode but there are some discrepancies.

Let's say if we move stack1 (1-2-3) to another stack2 by doing pop() and push() three times, do we consider O(1) extra space since we delete one element from stack1 and create an element in stack2 thus no extra space is used? Or we consider it as O(n) space complexity due to we created a stack2 same size as stack1(but stack1 is gone..)?

Thanks in advance!

SLAM_DK
  • 3
  • 2

1 Answers1

0

That depends on how your stack is implemented. That is, what is the backing store?

If the stack is implemented as an array, then the stack will be initialized with some default capacity, and a current count of 0. Something like:

s = int[10];
count = 0;

When you push something onto the stack, the item is added and the count is incremented to 1. If you try to push more items than the array will hold, then the array is extended. (Actually, probably a new array is allocated and the existing contents copied to it.)

When you pop something, count is reduced by 1, and s[count] is returned. But the array allocation is not changed.

So if the stack is implemented as an array, then copying from one stack to another will require O(n) extra space. At least temporarily.

If the stack is implemented as a linked list:

head > node > node > node ...

Then typically pop just returns head, and the new head is the node that head previously pointed to. In this case, the memory occupied by the stack is reduced with each pop. Of course, adding to the new stack increases the memory allocation by the same amount. So if the stack is implemented as a linked list, then copying from one stack to another is O(1) extra space.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Thanks for your answer. I'm considering the stack as `std::stack` in c++. I think it is more like your linked list case where instead of fixed size, it reduces the memory allocation by some amount by doing `pop`, and creates the same amount memory in another stack by doing `push`. Would you agree with my point? – SLAM_DK Aug 08 '19 at 09:16
  • @SLAM_DK According to the [std::stack documentation](https://en.cppreference.com/w/cpp/container/stack), the type of sequence container used can be specified. Of the standard container types, `std::vector` (implemented as an array), `std::deque` (a sequence of fixed-size arrays), and `std::list` (typically a linked list) all can be used. So, again, "it depends." If you don't specify a container when you create the stack, `std::deque` is used. I'm not familiar with the `deque` implementation. I don't know whether or how it deallocates space as the number of items in the stack is reduced. – Jim Mischel Aug 08 '19 at 16:25
  • I think I was oversimplifying the problem. After some search, [this](https://stackoverflow.com/questions/5834754/stddeque-does-not-release-memory-until-program-exits) tells me it does not deallocates space in `deque`, therefore O(n) space complexity is needed when moving elements from one stack to another. Not sure if I want to go this deep in an algorithm interview.. Again, thanks for your answer. – SLAM_DK Aug 08 '19 at 19:50