Data structure for non-overlapping ranges within a single dimension

前端 未结 8 1281
再見小時候
再見小時候 2020-12-30 11:26

I need a data structure that can store non-overlapping ranges within a single dimension. The entire range of the dimension need not be completely covered.

An example

8条回答
  •  别那么骄傲
    2020-12-30 12:00

    A lot depends on what you'll be doing with the data, and therefore which operations need to be efficient. However, I'd consider a doubly linked list of Ranges with logic in the setters of Start and End to check whether it now overlaps its neighbours, and to shrink them if so (or throw an exception, or however you want to handle an attempted overlap).

    That gives a nice simple linked list of booked periods to read, but no container responsible for maintaining the no-overlap rule.

提交回复
热议问题