We all know that std::vector::push_back
has constant (amortized) complexity.
It is constant because we say that the amortization cost is negligible, doubling every time. With reallocation being linear complexity.
Let's change our std::vector
interface a bit to force reallocation in different fun ways.
Reallocate at every push_back
Is push_back
still O(1)
(amortized) if we reallocated on every push_back
?
At every push_back
we go through all the items in the vector
I guess the constant part would drop here and both push_back
and reallocation are O(N)
Reallocate only on odd numbered push_back
Is push_back
still O(1)
(amortized) if we reallocate on every other push_back
?
Compared to the other variant, would push_back
still be linear O(N)
? Or can we still assume it's constant amortized?