Imagine you're working on multiple tasks on your computer with n windows open, numbered from 1 to n. Just like in real operating systems, you can use Alt+Tab to navigate between windows, which brings the selected window to the front (top of the stack).
You're given an array windows representing the initial order of windows, where the first element is at the top (most recently used) and the last element is at the bottom (least recently used).
You're also given an array queries where each queries[i] represents a window that gets brought to the top through Alt+Tab navigation.
Your task: Return the final state of the windows array after processing all queries, simulating the behavior of a window manager's Alt+Tab functionality.
Think of this as implementing the core logic behind your operating system's window switching mechanism!
Input & Output
Visualization
Time & Space Complexity
While lookup is O(1), we still need O(n) time to shift elements for each query. Total: O(m) queries ร O(n) shifting = O(nรm)
Hash map stores the position of each of the n windows
Constraints
- 1 โค n โค 103
- 1 โค windows[i] โค n
- All windows[i] are unique
- 1 โค queries.length โค 103
- 1 โค queries[i] โค n
- queries[i] will always exist in windows