what is the meaning of O(1), O(n), O(n*n) memory? [duplicate]

As xmoex said: o(1) constitutes a constant memory usage. So amount of input is inconsequential. o(n) constitutes a linear memory usage. So more input means linearly more memory. o(n*n) constitutes a quadratic memory usage. So more input means quadratically more memory (x^2 on average. This measure of memory complexity in most cases is completely independent … Read more

Is this algorithm linear?

This looks like a really neat idea, but sadly I believe the worst case behaviour is O(n^2). Here is my attempt at a counterexample. (I’m not a mathematician so please forgive my use of Python instead of equations to express my ideas!) Consider the string with 4K+1 symbols s=”a”*K+’X’+’a’*3*K This will have borders[1:] = range(K)*2+[K]*(2*K+1) … Read more

Given an array, can I find in O(n) the longest range, whose endpoints are the greatest values in the range?

A simple stack based solution. Iterate over the array left to right, with the stack holding elements (technically, indexes, but use values for comparisons) that are either: The largest from the left (i.e. no larger or equal element between the start of the array and the element) The largest since the previous element on the … Read more

A data structure supporting O(1) random access and worst-case O(1) append?

There is a beautiful structure called an extendible array that has worst-case O(1) insertion and O(n) memory overhead (that is, it’s asymptotically comparable to a dynamic array, but has O(1) worst-case insertion). The trick is to take the approach that the vector uses – doubling and copying – but to make the copying lazy. For … Read more