- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
范围相交是一个简单但不平凡的问题。
已经回答过两次了:
第一个解决方案是 O(n),第二个解决方案是针对数据库(当然小于 O(n))。
我有同样的问题,但是对于一个很大的 n 并且我不在数据库中。
这个问题似乎和 Store 2D points for quick retrieval of those inside a rectangle 很相似但我看不出它是如何映射的。
那么,您会将范围集存储在什么数据结构中,这样对范围的搜索成本低于 O(n)? (使用可用于 Java 的库的额外功劳)
编辑:
我想获取所有相交范围的子集,这意味着搜索范围可以与多个范围相交。
Java中需要小于O(n)的方法是:
public class RangeSet {
....
public Set<Range> intersects(Range range);
....
}
其中 Range 只是一个包含一对 int start 和 end 的类。
这不是一个不可能的问题,我已经有了解决方案,我只是想看看是否有更标准/更简单的方法来做到这一点
最佳答案
标准方法是使用 interval tree .
In computer science, an interval tree is a tree data structure to hold intervals. Specifically, it allows one to efficiently find all intervals that overlap with any given interval or point. It is often used for windowing queries, for instance, to find all roads on a computerized map inside a rectangular viewport, or to find all visible elements inside a three-dimensional scene. A similar data structure is the segment tree.
The trivial solution is to visit each interval and test whether it intersects the given point or interval, which requires O(n) time, where n is the number of intervals in the collection. Since a query may return all intervals, for example if the query is a large interval intersecting all intervals in the collection, this is asymptotically optimal; however, we can do better by considering output-sensitive algorithms, where the runtime is expressed in terms of m, the number of intervals produced by the query. Interval trees have a query time of O(log n + m) and an initial creation time of O(n log n), while limiting memory consumption to O(n). After creation, interval trees may be dynamic, allowing efficient insertion and deletion of an interval in O(log n). If the endpoints of intervals are within a small integer range (e.g., in the range [1,...,O(n)]), faster data structures exist[1] with preprocessing time O(n) and query time O(1+m) for reporting m intervals containing a given query point.
关于java - 比 O(n) 更好的范围相交算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/303591/
我有一个标记的个体列表(Mark 列),这些个体在河流(LocStart 和 LocEnd)范围内的不同年份(Year 列)被捕获。在河上的位置以米为单位。 我想知道一个被标记的个体是否在不同年份之间
我目前正在开发一个事件管理系统,其中数据库存储时隙,如下所示: SlotId | DateTime | Duration -------+-------------------
给定同一个圆的两个圆段:A=[a1, a2] 和 B=[b1, b2],其中: a1、a2、b1、b2 的值介于 -inf 和 +inf 之间 a1 overlap A=[ -45°, 45
试图让两个数据集相交,但我做不到。例如,在我下面的代码中,相交 mySet 和 mySet2 应该产生“1”,因为它们在它们的集合中都有一个值“1”。 var mySet = new Set(); v
给定同一个圆的两个圆段:A=[a1, a2] 和 B=[b1, b2],其中: a1、a2、b1、b2 的值介于 -inf 和 +inf 之间 a1 overlap A=[ -45°, 45
我有两个要相交的集合,并对匹配元素执行求和运算。 例如集合是(在伪代码中): col1 = { {"A", 5}, {"B", 3}, {"C", 2} } col2 = { {"B", 1}, {"
我有一个使用 -setFrameRotation 旋转的 NSView。 (这是必要的,因为 View 响应鼠标事件,如果您仅使用旋转的 NSAffineTransform 绘制 View ,则不会获
我在网上找到了这段代码,显然它对其他人有效,但对我无效?我不知道哪里错了。我做了一个简单的例子,并将我的 Range1 和 Range 2 设为 excel 中的某些单元格, 另外,我想知道是否有办法
确定直线是否与矩形相交的最有效方法是什么? 我正在寻找类似的东西: CGPoint startLine = CGPointMake(5.0f,5.0f); CGPoint endLine = CGPo
QPolygonF有与其他 QPolygonF 并集、相交和相减的方法,但我需要与 QLineF 执行相交测试。 API 中似乎缺少此功能。 我想我可以做这样的事情: if (polygon .con
所以,我尝试使用矩形在游戏中对墙壁进行碰撞,我决定尝试使用 ArrayList 来存储每面墙的矩形,然后我将整个 field 设为一面墙,并且所有我想做的是删除三堵墙,所以我正在执行 shapeLis
鉴于这两个表/集合具有不同的项目组, 我如何找到 set1 中的哪些组跨越 set2 中的多个组? 如何找到 set1 中的组不能被 set2 中的单个组覆盖? 例如对于下表,A (1,2,5) 是唯
我在 Hive 中有两个字符串数组,例如 {'value1','value2','value3'} {'value1', 'value2'} 我想合并没有重复的数组,结果: {'value1','va
谁能给我 tsql 来查找包含起始日期和截止日期的日期。 select * from empc where DateFrom >= p_todate AND DateTo = p_fromdate 关
我正在尝试从分桶列中获取子集,然后获取交集。 这将从原始表中选择其他列。 我也对系列过滤持开放态度。 下面的代码报告 col1 不存在 - 不确定这是正确的方法。 WITH ranges AS (
SELECT friend_id FROM friendships WHERE user_id = 1; Returns: +-----------+ | friend_id | +---------
似乎无法在任何地方找到这个问题的答案。 我的游戏在用户触摸屏幕时开始,手指必须停留在一条路径内,如果它触摸/与边缘相交,那么我希望它运行 [self gameover] 方法。 边缘将是一个 UIIm
我有两个 RDD,一个非常大,另一个小得多。我想用小 RDD 的键在大 RDD 中找到所有唯一的元组。 大 RDD 太大,我必须避免完全洗牌 小型 RDD 也足够大,我无法广播它。我也许可以广播它的
所以我有两个函数的代码。第一个打印一个空的 20x20 板,第二个打印中间的一个字。现在我正在尝试编写一个函数来检查输入的单词是否会与同一字母的另一个单词(如填字游戏)相交。这是前两个函数的代码(此处
我正在一个网站上进行培训,该网站要求我制作一个程序,该程序将询问两个矩形的坐标并检查矩形是否相交。然后,当我发送程序时,网站会对其进行几次测试。它要求 A 矩形的 x 最小值、x 最大值、y 最小值和
我是一名优秀的程序员,十分优秀!