Leetcode Problem 1122. Relative Sort Array

1122. Relative Sort Array

Leetcode Solutions

Counting Sort with Custom Order

  1. Initialize a count array of size 1001 (since the range is 0 to 1000) to 0.
  2. Iterate over arr1 and increment the count of each number in the count array.
  3. Initialize an index variable to keep track of the position in arr1.
  4. Iterate over arr2 and for each number, place it in arr1 as many times as its count and decrement the count each time.
  5. Iterate over the count array and for each number with a non-zero count, place it in arr1 in ascending order.
  6. Return the sorted arr1.
UML Thumbnail

Custom Sorting with HashMap and List

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...