我有範圍假設最佳算法找到交點和範圍的重疊,並存儲所得到的範圍內設定
- 1-10
- 20-40
- 30-50
- 55-65
- 65-80
- 75-90
- 95-100
正如在這個例子中20-40和30-50相交而不是存儲兩個我需要將它存儲爲20-50。
然後,而不是55-65,65-80和75-90我想單獨存儲55-90。
所以結果集將是這樣
- 1-10
- 20-50
- 55-90
- 95-100
我在這些值redis和我用Java存儲它們的結構是數組,它是一個起始數組和結束數組。
我的解決辦法:
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
,我發現這是爲O(n log n)的有沒有更好的算法來做到這一點?
在Java和redis中的數據結構中的任何建議以及用於處理它的方法或算法都很好。
感謝
它們是否總是排序?如果是這樣,一個不能假設的庫比知情算法慢。 –
@ Frederik.L幾乎總是如此,但除非性能問題(實際上需要數百萬個範圍才能成爲問題),經過測試的強大的庫代碼始終優於自制程序。 –
@BoristheSpider同意,但OP要求最佳算法。他沒有詢問最穩定和最符合生產要求的方式。只是我的兩分錢。 –