简体   繁体   English

给定一个对象A和一个对象L列表,如何在不测试所有情况的情况下查找L上的哪些对象是A的克隆?

[英]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)找到元素,但是:

  • A) you need elements with one variable to compare A)您需要一个变量的元素进行比较
  • B) sorted list by that variable B)按该变量排序列表

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.

相关问题 如何找到 forms 给定长度 N 数组的范围 [LR] 内的算术级数的最大连续段的长度? - How to find length of largest contiguous segment which forms an arithmetic progression in range [L R] of a given array of length N? 给定一个标记为1到N的列表L,以及一个将“随机”元素从考虑中删除的过程,如何有效地跟踪min(L)? - Given a list L labeled 1 to N, and a process that “removes” a random element from consideration, how can one efficiently keep track of min(L)? 查找范围 [L, R] 中其二进制表示包含给定模式的所有数字 - Find all numbers in the range [L, R] whose binary representation contains a given pattern 展示给定任何常规语言L的算法,该算法确定L = L * - Exhibiting an algorithm that determines if L = L*, given any regular language L 如何在矩阵中找到最大的L sum? - how to find the maximum L sum in a matrix? 如何在给定图片上找到黄色物体? - How to find yellow objects on given picture? 在分区对象时如何遍历所有情况 - How to iterate through all cases when partitioning objects 给定两个集合 (A,B) 和对 (a,b) 的列表 L st SA 和 SB 的笛卡尔积在 L 中,如何获得两个子集 (SA,SB)? - How can I get the two subsets (SA,SB) given two sets (A,B) and a list L of pairs (a,b) s.t. the Cartesian product of SA and SB is in L? 如何计算从列表L(所有子串的列表)中选择k个相等子串的方式的数量 - How to count the number of ways of choosing of k equal substrings from a List L(the list of All Substrings) 求(L,t)-团 - Find (L,t)-clump
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM