LeetCode-in-Java

3668. Restore Finishing Order

Easy

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

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

Example 1:

Input: order = [3,1,2,5,4], friends = [1,3,4]

Output: [3,1,4]

Explanation:

The finishing order is [**3**, **1**, 2, 5, **4**]. Therefore, the finishing order of your friends is [3, 1, 4].

Example 2:

Input: order = [1,4,5,3,2], friends = [2,5]

Output: [5,2]

Explanation:

The finishing order is [1, 4, **5**, 3, **2**]. Therefore, the finishing order of your friends is [5, 2].

Constraints:

Solution

public class Solution {
    public int[] recoverOrder(int[] order, int[] friends) {
        int[] rs = new int[friends.length];
        int index = 0;
        for (int k : order) {
            for (int friend : friends) {
                if (k == friend) {
                    rs[index] = k;
                    index++;
                    break;
                }
            }
        }
        return rs;
    }
}