1122. Relative Sort Array - Explanation

Problem Link



1. Brute Force

class Solution:
    def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
        res = []

        for num2 in arr2:
            for i, num1 in enumerate(arr1):
                if num1 == num2:
                    res.append(num1)
                    arr1[i] = -1

        arr1.sort()
        for i in range(len(res), len(arr1)):
            res.append(arr1[i])

        return res

Time & Space Complexity

  • Time complexity: O(mn+nlogn)O(m * n + n \log n)
  • Space complexity:
    • O(1)O(1) or O(n)O(n) depending on the sorting algorithm.
    • O(n)O(n) space for the output list.

Where nn is the size of the array arr1arr1, and mm is the size of the array arr2arr2.


2. Hash Map

class Solution:
    def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
        arr2_set = set(arr2)
        arr1_count = defaultdict(int)
        end = []

        for num in arr1:
            if num not in arr2_set:
                end.append(num)
            arr1_count[num] += 1
        end.sort()

        res = []
        for num in arr2:
            for _ in range(arr1_count[num]):
                res.append(num)
        return res + end

Time & Space Complexity

  • Time complexity: O(n+m+nlogn)O(n + m + n \log n)
  • Space complexity: O(n)O(n)

Where nn is the size of the array arr1arr1, and mm is the size of the array arr2arr2.


3. Hash Map (Optimal)

class Solution:
    def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
        count = {}
        for num in arr1:
            count[num] = count.get(num, 0) + 1

        res = []
        for num in arr2:
            res += [num] * count.pop(num)

        for num in sorted(count):
            res += [num] * count[num]

        return res

Time & Space Complexity

  • Time complexity: O(n+m+nlogn)O(n + m + n \log n)
  • Space complexity: O(n)O(n)

Where nn is the size of the array arr1arr1, and mm is the size of the array arr2arr2.


4. Counting Sort

class Solution:
    def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
        max_val = max(arr1)
        count = [0] * (max_val + 1)

        for num in arr1:
            count[num] += 1

        res = []
        for num in arr2:
            res += [num] * count[num]
            count[num] = 0

        for num in range(len(count)):
            res += [num] * count[num]

        return res

Time & Space Complexity

  • Time complexity: O(n+m+M)O(n + m + M)
  • Space complexity:
    • O(M)O(M) extra space.
    • O(n)O(n) space for the output list.

Where nn is the size of the array arr1arr1, mm is the size of the array arr2arr2, and MM is the maximum value in the array arr1arr1.


5. Custom Sort

class Solution:
    def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
        index = {num: i for i, num in enumerate(arr2)}
        return sorted(arr1, key=lambda x: (index.get(x, 1000 + x)))

Time & Space Complexity

  • Time complexity: O(n+m+nlogn)O(n + m + n \log n)
  • Space complexity:
    • O(m)O(m) extra space.
    • O(n)O(n) space for the output list.

Where nn is the size of the array arr1arr1, and mm is the size of the array arr2arr2.