#3237
Medium Algorithms Alt and tab simulation
Array Hash Table Simulation
52.6% acceptance
Mar 31, 2026
15
6
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.
Solution
Rust
Time O(n)
Space O(n)
use std::collections::HashSet;
impl Solution {
pub fn simulation_result(windows: Vec<i32>, queries: Vec<i32>) -> Vec<i32> {
let mut seen = HashSet::new();
let mut result = Vec::new();
for &q in queries.iter().rev() {
if seen.insert(q) {
result.push(q);
}
}
for &w in &windows {
if seen.insert(w) {
result.push(w);
}
}
result
}
}