[LeetCode] Alt and Tab Simulation

3237. Alt and Tab Simulation

There are n windows open numbered from 1 to n, we want to simulate using alt + tab to navigate between the windows.

You are given an array windows which contains the initial order of the windows (the first element is at the top and the last one is at the bottom).

You are also given an array queries where for each query, the window queries[i] is brought to the top.

Return the final state of the array windows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
vector<int> simulationResult(vector<int>& windows, vector<int>& queries) {
vector<int> res;
unordered_set<int> seen;
for(int i = queries.size() - 1; i >= 0; i--) {
int w = queries[i];
if(seen.count(w)) continue;
seen.insert(w);
res.push_back(w);
}
for(int i = 0; i < windows.size(); i++) {
if(seen.count(windows[i])) continue;
res.push_back(windows[i]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/22/PS/LeetCode/alt-and-tab-simulation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.