A data structure that supports the most common range-based element query

I am looking for a data structure with which I can find the most common number (among an array of numbers) in a given variable range.

Consider the following array based on 1:

1 2 3 1 1 3 3 3 3 1 1 1 1

If I request a range (1,4), the data structure should reassign 1, which happens twice. A few other examples:

(1,13) = 1

(4.9) = 3

(2,2) = 2

(1,3) = 1 (all 1,2,3 meet once, therefore we return the first / smallest, not so important at the moment)

I searched, but did not find anything like it. I am looking for (ideally) a data structure with minimal space requirements, fast preprocessing and / or query complexity.

Thanks in advance!

+5
2

N - , M - .

: n, .


1:
  • Spacial: O (1) O (M)
  • : O (1) O (n + M)

, .


2:
  • Spacial: O (M * N) O (1)
  • : O (M * N) O (min (n, M))

, x, x .

, , x 2 , x . x . n < M , x.


3:
  • Spacial: O (N) O (1)
  • : O (N) O (min (n, M) * log (n))

x , x. , x .

, , x 2 , x : O (log (N)) x / . , , O (log (N)).

+1

, node {value → frequency} , .

- , .

:

  • "" .
  • , ( , M) . , , , .
  • , , , node

UPDATE: , M N :

  • O (N log N) - , node M, node
  • O (M log N) - O (log N) M O (M)
  • O (N) - . 2N/M M. 2 N/M , 0,5N/M , 0,25N/M .....
0

All Articles