append
in amortized constant time. What is the space overhead?
Now we also want to be able to shrink the array by one from the end,
i.e., a reverse operation to append
. It could make sense
to refer to the operations as push
and pop
instead.
Decide on a strategy for when to reallocate to a smaller array so
that the space overhead is O(n) and such that you can define
a potential function and show that push
and pop
(any mix of these) are both amortized constant.
push
and pop
,
but we want all times to be worst-case instead of amortized.
To that end, we must anticipate reallocation to smaller and larger
arrays and get it done gradually.
This will increase our space overhead, but it should still be O(n).
Devise an algorithm and argue that it works.
(Doing something like this is referred to as global rebuilding.)
What is your space overhead?