694. Alt and Tab Simulation

Given an array windows, representing n open windows in their initial order (where the first element is at the top and the last element is at the bottom), you want to simulate the "Alt + Tab" behavior for switching between windows.

You are also given an array queries, where each queries[i] represents a window that should be moved to the top.

Return the final order of the windows after processing all queries.

Example 1:

Input: windows = [2, 1, 4, 3], queries = [3, 4, 1]  

Output: [1, 4, 3, 2]

Explanation:

Initial order: [2, 1, 4, 3]

Move window 3 to the top: [3, 2, 1, 4]

Move window 4 to the top: [4, 3, 2, 1]

Move window 1 to the top: [1, 4, 3, 2]

Example 2:

Input: windows = [5, 3, 1, 2, 4], queries = [2, 5, 3]  

Output: [3, 5, 2, 1, 4]

Explanation:

Initial order: [5, 3, 1, 2, 4]

Move window 2 to the top: [2, 5, 3, 1, 4]

Move window 5 to the top: [5, 2, 3, 1, 4]

Move window 3 to the top: [3, 5, 2, 1, 4]

Now Your Turn!

Pick the correct output for the given input

Input: windows = [6, 2, 5, 1, 3, 4], queries = [4, 6, 1]  

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ n == windows.length ≤ 105
  • windows is a permutation of [1, n]
  • 1 ≤ queries.length ≤ 105
  • 1 ≤ queries[i] ≤ n

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<int> simulationResult(vector<int>& windows, vector<int>& queries) {
}
};
Test Case

Input:

Queries
Windows