A range intersection algorithm better than O(n)?

前端 未结 9 743
悲&欢浪女
悲&欢浪女 2020-12-04 18:27

Range intersection is a simple, but non-trivial problem.

Its has been answered twice already:

  • Find number range intersection
  • Comparing date ra
9条回答
  •  爱一瞬间的悲伤
    2020-12-04 19:16

    Sounds like you need a class that implements the SortedSet interface. TreeSet is the implementation that ships with the core API.

    Have one set holding the ranges sorted by lowest value, and one sorted by highest value.

    You can then implement the equivalent of the database algorithm using the in-memory sets.

    As for whether this is actually faster than O(n), I couldn't say.

提交回复
热议问题