|
|
|
|
|
by boomlinde
2705 days ago
|
|
> Prepending would be O(1) because it's creating a new array instead of prepending in place. No, you still need to copy the old array to the new array. FWIW, Ruby may already be allocating some space before and after the array to accommodate a few pre-/appendages. |
|
That's just a lock (nontrivial but O(1)) and a memcpy (technically O(n) but trivial, and O(1) for the common case if it's implemented with vector instructions), plus in any event the sums-of-neighbors method has to be at least O(n) on an idealized Von Neumann machine because it must read every element of the source array and also write every element of the destination.