简体   繁体   English

奇怪的一个算法

[英]odd one out algorithm

given a set of N numbers, i am supposed to find the odd one out. 给定一组N个数字,我应该找到奇数的数字。 now N is an odd number, and the way to determine the 'odd one out' is by pairing the given numbers together, and eventually you'll be left with one number, which is the 'odd one out' . 现在N是一个奇数,确定'奇数一个'的方法是将给定的数字组合在一起,最后你将留下一个数字,即'奇数一个'。

The numbers are paired according to the distance between them. 数字根据它们之间的距离进行配对。 So first, two numbers with the least distance between them are picked from the given set of numbers and paired together. 首先,从给定的一组数字中挑选两个距离最小的数字并将它们配对在一起。 This leaves us with N-2 numbers in the set. 这使我们在集合中留下了N-2个数字。 The process is repeated until there is only 1 number left. 重复该过程,直到只剩下1个数字。

example: {1,4,3} 例如:{1,4,3}

the distance between 1 and 3 is 2 and the distance between 3 and 4 is just 1. So 3 and 4 are paired which leaves us with 1 unpaired making it the odd man. 1到3之间的距离是2,3到4之间的距离只有1.所以3和4是配对的,这使得我们有1个不成对,使它成为奇怪的人。

So far all i could think of is to sort the given list, and find the difference between each of the numbers and eliminate pairs, starting with the pair with the least distance between them. 到目前为止,我所能想到的是对给定列表进行排序,找出每个数字之间的差异并消除对,从它们之间距离最小的对开始。 This would eventually land me with the 'odd one out' but the problem has to be solved with an algorithm that has a complexity less than O(N^2). 这最终会让我感到“奇怪的一个”,但问题必须通过复杂度小于O(N ^ 2)的算法来解决。 Some help in the right direction would be greatly appreciated. 一些正确方向的帮助将不胜感激。 Thank you 谢谢

one more example: {1,3,4,6,10} 还有一个例子:{1,3,4,6,10}

pair with least difference 3,4 eliminate pair -> {1,6,10} pair with least difference 6,10 eliminate pair -> {1} is the odd one out 对差异最小3,4对消除对 - > {1,6,10}对差异最小6,10消除对 - > {1}是奇数对

another example {2,4,1,10,8,9,6} 另一个例子{2,4,1,10,8,9,6}

pair with least difference (1,2) (8,9) and (9,10). 具有最小差异(1,2)(8,9)和(9,10)的对。 eliminate (1,2) and (8,9) or (10,9) doesn't matter (for similar distances result can go either way; unpredictable) lets pick (8,9) -> {4,10,6} 消除(1,2)和(8,9)或(10,9)无关紧要(类似的距离结果可以是任何一种方式;不可预测)让pick(8,9) - > {4,10,6}

next eliminate (4,6) --> {10} is the odd one out note: could have picked (9,10) instead of (8,9) as well. 下一个消除(4,6) - > {10}是奇怪的一个注释:本来可以选择(9,10)而不是(8,9)。

i hope this clears up things 我希望这可以解决问题

One possible O(nlogn) solution is as follows: 一种可能的O(nlogn)解决方案如下:

  1. Sort the array (in O(nlogn) time). 对数组进行排序(在O(nlogn)时间内)。
  2. Compute the differences between adjacent elements in the sorted array. 计算排序数组中相邻元素之间的差异。
  3. Store the difference values (along with the respective pairs) in a min-heap. 将差值(以及相应的对)存储在最小堆中。
  4. Pop out the top pair from the min-heap. 弹出最小堆中的顶对。 For every popped out pair, you will have to add another pair to the heap. 对于每个弹出的对,您将不得不向堆添加另一对。 This will be corresponding to the adjacent elements from the popped out pair. 这将对应于弹出对中的相邻元素。 For example if the array was {...., 3, 5, 6, 9, ....} and the popped out pair was {5,6}, then add another pair in the heap {3,9} with a difference value of 6. 例如,如果数组是{....,3,5,6,9,....}并且弹出的对是{5,6},那么在堆{3,9}中添加另一对差值为6。
  5. Also, keep track of the elements already popped out (probably in a hash table) and simply reject any popped pair for which any of the elements is already popped out. 此外,跟踪已经弹出的元素(可能在哈希表中),并简单地拒绝任何已弹出任何元素的弹出对。
  6. Once the heap is empty, the element that was not included in any valid popped out pair shall be the odd one out. 一旦堆为空,未包含在任何有效弹出对中的元素应为奇数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM