Leetcode Problem 1122. Relative Sort Array
1122. Relative Sort Array
AI Mock Interview
Leetcode Solutions
Counting Sort with Custom Order
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize a count array of size 1001 (since the range is 0 to 1000) to 0.
Iterate over
arr1
and increment the count of each number in the count array.
Initialize an index variable to keep track of the position in
arr1
.
Iterate over
arr2
and for each number, place it in
arr1
as many times as its count and decrement the count each time.
Iterate over the count array and for each number with a non-zero count, place it in
arr1
in ascending order.
Return the sorted
arr1
.
Custom Sorting with HashMap and List
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...