从范围列表中查找重叠范围 [英] Finding overlapping Range from Range List

查看:52
本文介绍了从范围列表中查找重叠范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是Java特定的问题,但是我必须用Java来实现,这就是为什么我用Java对其进行了标记.

This is not a Java specific question, but I have to implement it in Java, that is why I tagged it with Java.

问题:

我有一个范围列表(在此示例中,由Stratpoint和Endpoint指定范围,我简单地使用int []):

I have a List of Ranges (a Range is specified by a Stratpoint and an Endpoint in this example i simple use an int[]):

在Java中:

List<int[2]> ranges = new ArrayList<>();
ranges.add(new int[]{1,100});
ranges.add(new int[]{30,95});
ranges.add(new int[]{10,60});
ranges.add(new int[]{15,25});
ranges.add(new int[]{33,66});
ranges.add(new int[]{20,50});
ranges.add(new int[]{51,100});
ranges.add(new int[]{25,70});

输出应该是一个范围,在大多数范围中都存在.在这种情况下,输出为 [33,50] .因为此范围在列表的6个范围之内.

The output should be a Range, that is present in the most Ranges. In this chase the output would be [33,50]. Because this Range is inside of 6 Ranges from the List.

希望您会理解我的问题.谢谢!

Hope you will understand my question. Thanks!

推荐答案

编辑:实际上,请参见

EDIT: Actually, just see this answer by @Dave, which is much more efficient.

编辑:不幸的是,我之前的代码有缺陷.例如,使用以下输入,它将输出范围99-100,而不是3-50.在此处尝试新代码.

EDIT: Unfortunately, my previous code is flawed. For example, with the below input, it outputs the range 99-100 instead of 3-50. Try the new code here.

Range(1, 100), Range(99, 100), Range(2, 50), Range(3, 90)


我写了另一种算法,但是这种算法速度较慢并且占用更多空间(时间和空间复杂度均为2 ^ n)


I wrote another algorithm, but this one is way slower and takes up more space (both time and space complexity are 2^n)

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆