I have ranges let's say
- 1-10
- 20-40
- 30-50
- 55-65
- 65-80
- 75-90
- 95-100
As in this example 20-40 and 30-50 intersects instead of storing both I need to store it as 20-50.
Then instead of 55-65,65-80 and 75-90 I want to store 55-90 alone.
So the result set would be like this
- 1-10
- 20-50
- 55-90
- 95-100
I have these values in redis and the Structure which I store them in Java are arrays a start array and end array.
My solution :
for int i =0; i< length-1 ; i++
for int j=i+1;j<length; j++
if start[i] <= start[j] && end[i] >= start[j]
store the min max in start and end array and remove the other two entries and proceed
I found this as O(n log n) is there any better algorithm to do this?
Any suggestions in the data structure both in Java and redis and the approach or algorithm for processing this would be great.
Thanks