Queries on a permutation with key

October 19, 2022

array-and-hashmap

Problem URL: Queries on a permutation with key

We will do the simulation. Fist we will create a list with all the permutation. For each query, we will find the index of the query value in the permutation, then we will move the value to the front of the permutation and add the index to the result.

class Solution:
    def processQueries(self, queries: List[int], m: int) -> List[int]:
        res = []
        mList = [i for i in range(1, m+1)]

        for num in queries:
            res.append(mList.index(num))

            mList.remove(num)
            mList.insert(0, num)

        return res

Time Complexity: O(n^2)
Space Complexity: O(n)