A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1122. Relative Sort Array, the solution in this repository is mainly a greedy solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
The notes already sitting in the source point us in the right direction:
- Code based on hints:
- Using a hashmap, we can map the values of arr2 to their position in arr2.
- After, we can use a custom sorting function.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are my_comparator, relativeSortArray.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Code based on hints:
02//Using a hashmap, we can map the values of arr2 to their position in arr2.
03//After, we can use a custom sorting function.
04//Runtime: 4 ms, faster than 92.33% of C++ online submissions for Relative Sort Array.
05//Memory Usage: 8.9 MB, less than 100.00% of C++ online submissions for Relative Sort Array.
06
07static map<int, int> m;
08
09static bool my_comparator(int a, int b){
10 return m[a] < m[b];
11}
12
13class Solution {
14public:
15 vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
16 vector<int>::iterator it;
17
18 for(int e : arr1){
19 it = find(arr2.begin(), arr2.end(), e);
20 if(it != arr2.end()){
21 m[e] = it - arr2.begin();
22 }else{
23 //the number not in arr2 should be put at last
24 //, adding "arr2.size()" so that m[e] is larger than m[x] for all x in arr2
25 m[e] = arr2.size() + e;
26 }
27 }
28
29 // for(auto it = m.begin(); it != m.end(); it++){
30 // cout << it->first << " " << it->second << endl;
31 // }
32
33 sort(arr1.begin(), arr1.end(), my_comparator);
34
35 return arr1;
36 }
37};
Cost