[LeetCode] Restore Finishing Order

3668. Restore Finishing Order

You are given an integer array order of length n and an integer array friends.

  • order contains every integer from 1 to n exactly once, representing the IDs of the participants of a race in their finishing order.
  • friends contains the IDs of your friends in the race sorted in strictly increasing order. Each ID in friends is guaranteed to appear in the order array.

Return an array containing your friends’ IDs in their finishing order.

1
2
3
4
5
6
7
8
class Solution {
public:
vector<int> recoverOrder(vector<int>& order, vector<int>& friends) {
unordered_set<int> us(begin(friends), end(friends));
for(int i = 0; auto& o : order) if(us.count(o)) friends[i++] = o;
return friends;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/09/03/PS/LeetCode/restore-finishing-order/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.