How to implement three stacks using a single array

后端 未结 17 616
情书的邮戳
情书的邮戳 2020-12-23 15:10

I came across this problem in an interview website. The problem asks for efficiently implement three stacks in a single array, such that no stack overflows until there is no

17条回答
  •  生来不讨喜
    2020-12-23 15:42

    Assuming that all array positions should be used to store values - I guess it depends on your definition of efficiency.

    If you do the two stack solution, place the third stack somewhere in the middle, and track both its bottom and top, then most operations will continue to be efficient, at a penalty of an expensive Move operation (of the third stack towards wherever free space remains, moving to the half way point of free space) whenever a collision occurs.

    It's certainly going to be quick to code and understand. What are our efficiency targets?

提交回复
热议问题