Queries on a Permutation With Key
Given the array queries of positive integers between 1 and m, process all queries[i] from i = 0 to i = queries.length - 1 according to the following rules:
- In the beginning, you have the permutation
P = [1, 2, 3, ..., m]. - For the current
i, find the position ofqueries[i]in the permutationP(indexing from 0) and then move this value to the beginning of the permutationP. - The position of
queries[i]inPis the result forqueries[i].
Return an array containing the result for the given queries.
Example 1
Input
queries = [3,1,2,1], m = 5Output
[2,1,2,1]Processing the queries moves each queried value to the front of
P, producing positions [2, 1, 2, 1].Example 2
Input
queries = [4,1,2,2], m = 4Output
[3,1,2,0]After each query is located and moved to the beginning of
P, the recorded positions are [3, 1, 2, 0].Constraints
- 1 <= m <= 10^3
- 1 <= queries.length <= m
- 1 <= queries[i] <= m