Talentd
FleetCode

3237. Alt and Tab Simulation

Medium54.3% acceptancePremium elsewhere, free here

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.

Example 1:

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

Output: [2,3,1]

Explanation:

Here is the window array after each query:

  • Initial order: [1,2,3]
  • After the first query: [3,1,2]
  • After the second query: [3,1,2]
  • After the last query: [2,3,1]

Example 2:

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

Output: [3,1,4,2]

Explanation:

Here is the window array after each query:

  • Initial order: [1,4,2,3]
  • After the first query: [4,1,2,3]
  • After the second query: [1,4,2,3]
  • After the last query: [3,1,4,2]

Constraints:

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

Hints open one at a time

Hint 1
Let’s answer the queries offline.
Hint 2
Start from the last query until you reach the first query.
Hint 3
Each time, append the value of that query to a ans array if it wasn’t already appended.
Hint 4
Then start from the beginning of windows and append to ans array if it wasn’t already appended.
3:26
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3]
[3,3,2]
Input
[1,4,2,3]
[4,1,3]
Your code is saved on this device.Solution