【问题标题】:Given sorted list how to create list of integers that wont fall in certain range in O(log(N)) time?给定排序列表如何创建在 O(log(N)) 时间内不会落在特定范围内的整数列表?
【发布时间】:2020-10-28 17:00:45
【问题描述】:

我有一个排序的整数数组列表(没有重复):

{1,2,4,5,8,12,15, 21,23,26,30,32,35,37,40,42,45,48,51,54}

假设给定范围是 [21-38](含),它必须从整数输出列表中排除。

那么输出应该包含不包含上述范围的整数列表。

{1,2,4,5,8,12,15,40,42,45,48,51,54}

我需要在 O(log(N)) 时间内完成此操作。

此时我能想到的方法是:

Approach1:
1) Find lower bound index using binary search in O(logN)
2) Find higher bound index using binary search in O(logN)
3) Then loop over the input list and add the integers to new list by 
considering above lower and higher bound index. But this third step takes O(n).

Approach2:

I also feel that the above approach of using binary search is not 
 great as we can directly iterate over original list to remove the 
  integers falling under the range without using binary search.

我们有更好的方法吗?

【问题讨论】:

  • 没有办法让新列表比 O(N) 更快
  • 不确定您是否正在对输出列表本身进行任何数据操作,但如果没有,您可以将此列表设为链表,只需将尾节点的 next 指针更新为第一个设置的头指针第二个列表。因此,这可以在对数时间内完成。但是,这实际上取决于您在返回输出列表之前尝试执行的操作的未来用例。
  • 感谢 Mbo 和 nice_dev。看起来没有办法使用 arraylist 来实现。
  • @SSK 使用arrayList on cose O(N),使用linkedList可以使用O(log(N)) 但是代码会很复杂所以我觉得O(N)不错。
  • @TongChen 感谢您的输入,我将使用 O(N) 方法2,

标签: java algorithm data-structures binary-search


【解决方案1】:

通过根据余数和移除的大小估计更好的选项,可以针对更好的情况优化元素的移除:

static List<Integer> removeFrom(List<Integer> sorted, int sv, int ev) {
    int i1 = Collections.binarySearch(sorted, sv);
    int i2 = Collections.binarySearch(sorted, ev);
    int from, to;
    if (i1 < i2) {
        from = i1;
        to = i2;
    } else {
        from = i2;
        to = i1;
    }
    System.out.printf("Removing values %d..%d%n", sv, ev);
    int size = sorted.size();
    int removeLength = to - from + 1;
    int remainLength = size - removeLength;
    if (removeLength < remainLength) {
        System.out.printf("Removing range: [%d, %d]%n", from, to);
        sorted.removeAll(sorted.subList(from, to + 1));
        return sorted;
    } else {
        List<Integer> result = new ArrayList<>();
        if (from > 0) {
            System.out.printf("Keeping head: [%d, %d]%n", 0, from);
            result.addAll(sorted.subList(0, from));
        }
        if (to < size - 1) {
            System.out.printf("Keeping tail: [%d, %d]%n", to, size);
            result.addAll(sorted.subList(to + 1, size));
        }
        return result;
    }
}

测试:

int[][] tests = {
    {1, 3},
    {7, 10},
    {3, 6},
    {2, 10},
    {1, 9},
    {2, 9}
};
for (int[] test : tests) {
    List<Integer> sorted = IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toList());
    System.out.println("result: " + removeFrom(sorted, test[0], test[1]) + "\n====\n");
}

输出

Removing values 1..3
Removing range: [0, 2]
result: [4, 5, 6, 7, 8, 9, 10]
====

Removing values 7..10
Removing range: [6, 9]
result: [1, 2, 3, 4, 5, 6]
====

Removing values 3..6
Removing range: [2, 5]
result: [1, 2, 7, 8, 9, 10]
====

Removing values 2..10
Keeping head: [0, 1]
result: [1]
====

Removing values 1..9
Keeping tail: [8, 10]
result: [10]
====

Removing values 2..9
Keeping head: [0, 1]
Keeping tail: [8, 10]
result: [1, 10]
====

因此,在最佳情况中,复杂度为 O(M),其中 M 是剩余部分的大小。

【讨论】:

    猜你喜欢
    • 2013-02-20
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 2015-07-06
    相关资源
    最近更新 更多