如何编写一个方法来查找范围数组中的所有交集?
我有与此类似的数组
[
[0, 10]**,
[1, 3]**,
[5, 11]**,
[14, 20]**,
[10, 11]**
]
** 表示包含数组中显示的开始和结束索引的对象
现在,交集是 [1, 3], [5,10], [10,11]
编写返回包含相交集的对象的方法的最佳方法是什么? (在我们进行过程中,可以将它们存储在一系列冲突的东西中)
我遇到的最大问题是如何做到这一点,以便每个对象都与彼此的对象进行比较?
有n!做到这一点的方法(我想,我对我的组合学有点生疏)
I have array similar to this
[
[0, 10]**,
[1, 3]**,
[5, 11]**,
[14, 20]**,
[10, 11]**
]
** Denotes an object containing the start and end indexes shown in the array
Now, the intersections are [1, 3], [5,10], [10,11]
What is the best way to write a method that returns the objects containing of the intersecting sets?
(Could just store them in an array of conflicted stuff as we go along)
The biggest issue I'm having is how do I do this such that each object is compared with eachother object?
there are n! ways to do this (i think, I'm a bit rusty on my combinatorics)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按开始时间对间隔进行排序(或者按开始时间对索引数组进行排序,这样就不会丢失索引)。
之后,您可以执行一次检测所有交叉点/冲突。
Sort the intervals by start time (or instead sort an array of indices by start time so you don't lose the indices).
After this you can do a single pass detecting all intersections/conflicts.