Get indexes of unique elements in two different arrays in linear time
-
05-11-2019 - |
質問
I have two array of the same set of elements, say for exemple: $a_1 = [x_1, x_2, …, x_n]$ and $a_2 = [y_1, y_2, …, y_n]$ so that $i \neq j \Rightarrow x_i \neq x_j$
Is it possible, in linear time, to construct the array $[(x_1, k_1), (x_2, k_2), …, (x_n, k_n)]$ so that $y_{k_i} = x_i$ ?
I found that with dictionnaries, I can get $O(n)$ in average and $O(n^2)$ in worst (hashtable) or $O(n\log n)$ in average and worst (balancing trees), but can't find a way to make it $O(n)$ in worst case.
We can suppose that the $x_i$ objects are integers, since it could represent memory pointers.
I found that it is possible if the set is $[\![1, n]\!]$ (just construct the array directly).
Thanks for your help.
正しい解決策はありません
所属していません cs.stackexchange