1122. Relative Sort Array
π© Easy
Question
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]Complexity
Code
Last updated
π© Easy
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]Last updated
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
ans, cnt = [], collections.Counter(arr1)
for i in arr2:
if cnt[i]: ans.extend([i] * cnt.pop(i))
rest = cnt.elements() # return iterator over elements repeat as its count
ans.extend(sorted(rest))
return ans