The solution generally depends on the range boundaries.
- If
max - min not so large (for example, you define the boundaries in [1..1024]), you can only use an array that points each X to the range list. For your example, the array should be:
ranges = [0: (1,10), 1: (5,10), 2: (10,12), 3: (7,13), 4: (15-20)]
points = [1: [0], 2: [0], 3: [0], 4: [0], 5: [0,1], ..., 7: [0,1,3] ,. ..10: [0,1,2,3], ... 15: [4], ... 20: [4], 21: [] ...]
So, in this case, you could quicly define the ranges for a particular X.
- You can use the Interval Tree - less efficiently, but frendlier memory (of course, more efficient than brute force solution).
source share