Skip to content

Practice Queries on a Permutation With Key

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1409
LeetCode Problem

Queries on a Permutation With Key

Problem Statement

Begin with P = [1, 2, ..., m]. Process queries in order. For each value, record its current zero-based index in P, then remove that value and insert it at the beginning of P, preserving the order of the others. Return the recorded indices.

Source: Queries on a Permutation With Key on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
queries = [3,1,2,1], m = 5
Output
[2,1,2,1]

# Example 2

Input
queries = [4,1,2,2], m = 4
Output
[3,1,2,0]

# Example 3

Input
queries = [7,5,5,8,3], m = 8
Output
[6,5,0,7,5]

Constraints

1 <= m <= 10^3
1 <= queries.length <= m
1 <= queries[i] <= m

Related LeetCode Problems

Practice more problems similar to Queries on a Permutation With Key.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.