Ctrl K

Window Order After Switching

Medium
There are n windows open on a screen, numbered 1 to n. The list windows gives their order from top to bottom: the first one is on top, the last one at the very bottom. The user clicks on the windows listed in clicks, one after another. A click brings that window to the very top, while the other windows keep their relative order. Return the order of the windows from top to bottom after all the clicks.
Example 1
windows=[2, 1, 3], clicks=[3, 2, 3] → [3, 2, 1]

[2, 1, 3] → raise 3: [3, 2, 1] → raise 2: [2, 3, 1] → raise 3: [3, 2, 1].

Example 2
windows=[4, 2, 1, 3], clicks=[4] → [4, 2, 1, 3]

Window 4 is already on top, so nothing changes.

Example 3
windows=[1, 2, 3, 4, 5], clicks=[5, 1, 4] → [4, 1, 5, 2, 3]
Constraints
  • 1 ≤ n = len(windows) ≤ 10⁵
  • windows is a permutation of 1 to n
  • 1 ≤ len(clicks) ≤ 10⁵
  • 1 ≤ clicks[i] ≤ n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHow Far the Rover Got MediumLast Step With a Lit Run MediumFully Dug Relics MediumA Chain of Replacements MediumRows That Match Columns Medium
loading Python…
Input
windows=[2, 1, 3], clicks=[3, 2, 3]
Expected
[3, 2, 1]

Run checks the first 3 tests in your browser. Submit checks all 78 tests on the server, hidden ones included.