Set with an average value of O (1) to add / remove and the worst max / min O (log n)

Is it possible to install a set in which the average add / remove operation is O (1) (this is typical for sets based on a hash table) and the worst max / min is less than O (n), maybe O (log n) (typical for wood based sets)?

upd hmm, it seems, in the simplest case, I can just re-check ALL N elements every time max / min disappears, and generally it gives me O (1). But I apply my algorithm to stock trading, where changes in the min / max area are much more likely, so I just don’t want to scan everything every time max or min disappear, I need something smarter than a full rescan, which gives O (n).

upd2 In my case, the set contains 100-300 elements. Changes in max / min elements are very likely, therefore max / min often changes. And I need to track max / min. I still want O (1) to add / remove.

+4
source share
2 answers

Well, you know that max/min < CONST, but all the elements are everything. Based on this, you can enter O(1)and O(k+n/k)find min / max 1 .

k, -. array[floor((x-MIN)/MAX-MIN)*k)] ( x = MAX). , , - n/k.
- .

findMax() : , - O (k) O(n/k), .

k:

k + n/k.

d(n+n/k)/dk = 1-n/k^2 = 0
n = k^2
k = sqrt(n)

O(sqrt(n) + n/sqrt(n)) = O(sqrt(n)) min/max , / O (1).

"reset" - max min, " " - , .
, MAX 2*max, MIN 1/2*min , reset "DS.


(1) , . - P(x)=P(y) x, y, .

+2

, , , . (, . -, .)

, . , , , . , max().

. . . , , , . , x, y, z :

x < y:  string(x) is now 0, string(y) is now 1
x < z:  string(z) is now 1
y < z:  string(y) is now 10, string(z) is now 11.

k - , . , 3 * n / 6 * k * n. 2 * n , , 6 * k, n n , 6 * k .

6 * k . (6 * k) - - , . . 2 ** (6 * k) , , , , (6 * k) - . n ( k) 2 * k , . , max() , , .

+6

All Articles