Leetcode 1122 - Relative Sort Array [11/6/24]

preview_player
Показать описание
Time Taken: ~10 mins
Tag: LeetCode Easy

High-level ideas:
- Counting sort, where we store frequency of each key in vector. countFreq[key] = freq
- Iterate through array 1 to populate countFreq vector
- Iterate through array 2 and access countFreq[key] for the frequency of current key. This is the number of times we have to add “key” to answer vector. Decrement countFreq[key] each time "key" is added to answer vector.
- Finally, iterate through countFreq vector to add elements whose frequency is still greater than zero. This ensures that the elements that do not exist in array 2 are added to the back of the answer vector in non-descending order.
Рекомендации по теме
join shbcf.ru