Here, we can leverage a hashtable to store the positions of the element in arr2
by their indices. This is done so while traversing arr1
we have the ordering/index of a number at near constant time.
Once we have the indices, we just feed that data to a sort comparator function, which does the sorting for us.
NOTE: For the numbers in `arr1` that aren't in `arr2`, we give a sort comparator value of `len(arr1) + x`, so it's out of bounds and is positioned after all coinciding numbers.
Code:
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since we are sorting an array.
-
Memory
The memory usage is since we use the
dict
hashtable to store positions.
— A