[英]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)解决方案如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.