[英]Given an object A and a list of objects L, how to find which objects on L are clones of A without testing all cases?
Using JavaScript notation: 使用JavaScript表示法:
A = {color:'red',size:8,type:'circle'};
L = [{color:'gray',size:15,type:'square'},
{color:'pink',size:4,type:'triangle'},
{color:'red',size:8,type:'circle'},
{color:'red',size:12,type:'circle'},
{color:'blue',size:10,type:'rectangle'}];
The answer for this case would be 2, because L[2] is identic to A. You could find the answer in O(n) by testing each possibility. 这种情况的答案是2,因为L [2]与A相同。您可以通过测试每种可能性在O(n)中找到答案。 What is a representation/algorithm that allows finding that answer faster?
什么是表示/算法可以更快地找到答案?
I would just create a HashMap and put all objects into the HashMap. 我只是创建一个HashMap并将所有对象放入HashMap中。 Also we would need to define a hash function which is function of data in object (something similar to overriding Object.hashcode() in java)
另外,我们需要定义一个哈希函数,该函数是对象中数据的函数(类似于覆盖Java中的Object.hashcode())
Suppose given array L is [B, C, D] where B, C and D are objects. 假设给定数组L为[B,C,D],其中B,C和D为对象。 Then HashMap would be {B=>1, C=>2, D=>3}.
那么HashMap将为{B => 1,C => 2,D => 3}。 Now suppose D is copy of A. So we would just lookup A in this map and get the answer.
现在假设D是A的副本。因此,我们只需要在此映射中查找A并获得答案即可。 Also as suggested by Eric P in comment, we would need to keep the hashmap updated with respect to any change in array L. This also can be done in O(1) for every operation in array L.
同样,正如Eric P在评论中建议的那样,我们将需要针对数组L中的任何更改更新哈希图。这也可以在数组L中的每个操作的O(1)中完成。
Cost of Looking up an object in the HashMap is O(1). 在HashMap中查找对象的成本为O(1)。 So we can achieve O(1) complexity.
这样我们就可以实现O(1)的复杂度。
I think it's not possible to do it faster than O(n)
with your preconditions. 我认为在您的前提下,不可能比
O(n)
更快。 It's possible to find element in O(logn)
using binary search, but: 可以使用二进制搜索在
O(logn)
找到元素,但是:
Maybe with some technics (ordering, skip lists, etc.) you can find answer faster than N iterations, but the worst case is O(n)
也许使用某些技术(排序,跳过列表等),您可以比N次迭代更快地找到答案,但最坏的情况是
O(n)
由于目标是查找所有属于A的克隆的对象,因此必须至少测试每个对象一次,以确定它是否是A的克隆,因此最小测试数为N。一次通过列表并测试每个对象执行N次测试,因此由于此方法是最少的测试次数,因此它是一种最佳方法。
first, I assume, that you are talking about array, not list. 首先,我假设您是在谈论数组,而不是列表。 the word 'list' is reserved for specific type of data structures, that has O(n) indexing comlexity, so meantime for any search in it is at least linear.
“列表”一词是为具有O(n)索引复杂性的特定类型的数据结构保留的,因此在其中进行任何搜索的时间至少都是线性的。
for unsorted array, the only algorithm is full scan with linear time. 对于未排序的数组,唯一的算法是线性时间的全扫描。 However, if array is sorted, you can use binary or interpolating search to get better time.
但是,如果对数组进行排序,则可以使用二进制或内插搜索来获得更好的时间。
The problem with sorted arrays is that they have linear insert time. 排序数组的问题在于它们具有线性插入时间。 No good.
不好。 So if you wish to update your set much and both update and search times are important, you should search for optimized container, that in c++ and haskell is called
Set
( set
template in set
header and Data.Set
module in containers
package respectively). 因此,如果您希望大量更新集合,并且更新和搜索时间都很重要,则应搜索优化的容器,在c ++和haskell
set
其称为Set
(分别位于set
头中的set
模板和containers
包中的Data.Set
模块)。 I dunno if there is any in JS. 我不知道JS中是否有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.