简体   繁体   English

在不到O(n)的时间内获得数据结构中元素之间的最小差异

[英]Get the smallest difference between elements in a data structure in less than O(n) time

Say I have some integers in a data structure. 假设我在数据结构中有一些整数。 When I insert new number into the data structure, I want to get the smallest difference between the new inserted elements and any other elements already in the data structure. 当我在数据结构中插入新数字时,我希望获得新插入的元素与数据结构中已有的任何其他元素之间的最小差异。 What data structure and algorithm should I use? 我应该使用什么数据结构和算法? AO(n) solution is trivial and I want better. AO(n)解决方案很简单,我想要更好。 Thanks. 谢谢。

One option would be to use a TreeSet (based on TreeMap ), which would require several O(lg n) operations. 一种选择是使用TreeSet (基于TreeMap ),这将需要几个O(lg n)操作。 The class exposes two methods which can be used to find the element which is closest to the value you wish to insert: 该类提供了两种方法,可用于找到最接近您要插入的值的元素:

public E ceiling(E e) 公共E上限(E e)
Returns the least element in this set greater than or equal to the given element, or null if there is no such element. 返回此集合中大于或等于给定元素的最小元素;如果没有此类元素,则返回null。

public E floor(E e) 公共E楼(E e)
Returns the greatest element in this set less than or equal to the given element, or null if there is no such element. 返回此集合中小于或等于给定元素的最大元素;如果没有此类元素,则返回null。

public static int findClosest(TreeSet set, Integer val) {
    if (set == null || set.size() == 0) {
        return -1;
    }

    // ceiling == 9 for input of 7
    // O(lg n) operation
    Integer ceiling = (Integer)set.ceiling(val);
    // floor = 6 for input of 7
    // O(lg n) operation
    Integer floor = (Integer)set.floor(val);

    if (ceiling == null) {
        return val - floor;
    }
    if (floor == null) {
        return ceiling - val;
    }

    return (val - floor > ceiling - val) ? ceiling - val : val - floor;
}

public static void main(String[] args) {
    TreeSet<Integer> ts = new TreeSet<>();
    ts.add(5);
    ts.add(1);
    ts.add(6);
    ts.add(9);
    ts.add(2);
    ts.add(3);

    int diff = findClosest(ts, 7);
    // closest is 6, so diff == 1
}

You could use a height-balanced binary search tree . 您可以使用高度平衡的二进制搜索树 These can be implemented with one of a number of data structures. 这些可以用许多数据结构之一来实现。 Insertions are usually O(log(n)) on average and looking up the one or two closest integers (on either side of the inserted value) will also be at most O(log(n)). 插入通常平均为O(log(n)),查找一个或两个最接近的整数(在插入值的任一侧)也最多为O(log(n))。

There may be other data structures that can do better (particularly if you can reasonably bound the integer values you need to deal with), but I can't think of one off the top of my head. 可能还有其他数据结构可以做得更好(特别是如果您可以合理地绑定需要处理的整数值),但是我想不到一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据结构为 select 数字,概率与其值成正比,时间复杂度小于 O(n) - Data structure to select number with probability proportional to its value in less than O(n) 有没有办法在少于 O(n) 的时间内连接 Java 字符串? - Is there a way to concatenate Java strings in less than O(n) time? 获得排序列表中相邻数组元素之间的最小差异 - Get Smallest Difference between Adjacent Array Elements in a Sorted List 是否可以在小于O(n log n)的时间内比较两个二叉树? - Is it possible to compare two binary trees in less than O(n log n) time? 如何从给定范围 Q 查询中创建数组 [1, 2, 3, 1],其中 N 个元素每个 [1, 3] [2, 3] [3, 4] 小于 O(QN)? - How to create array [1, 2, 3, 1] from given range Q queries with N elements each [1, 3] [2, 3] [3, 4] in less than O(QN)? 使用数据结构在O(n)中解决这个问题 - Using a data structure to solve this in O(n) 从排序数组中查找小于O(n)的唯一数字 - Finding unique numbers from sorted array in less than O(n) 找到小于O(N)的序列的第n项 - Find nth term of a sequence in less than O(N) 是否可以使嵌套的for循环小于O(N ^ 2)? - Is it possible to make this nested for loop less than O(N^2)? 在小于 O(n^2) 的范围内对给定的整数列表进行排名 - Ranking a given list of integers in less than O(n^2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM