describe

Given the array queries of positive integers between 1 and m, you have to 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 of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i].

Return an array containing the result for the given queries.

Example 1:

Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For I =0: Queries [I]= 1, P=[1,2,3,4,5] Then we move 3 to the beginning of P modifier =[3,1,2,4,5]. For I =1: Queries [I]=1, P=[3,1,2,4,5] Then we move 1 to the beginning of P modifier =[1,3,2,4,5]. For I =2: Queries [I]=2, P=[1,3,2,4,5], Then we move 2 to the beginning of P modifier =[2,1,3,4,5]. For I =3: Queries [I]=1, P=[2,1,3,4,5], Then we move 1 to the beginning of P modifier =[1,2,3,4,5]. Therefore, The array containing the result is [2,1,2,1].Copy the code

Example 2:

Input: queries = [4,1,2,2], m = 4
Output: [3,1,2,0]
Copy the code

Example 3:

Input: the queries =,5,5,8,3 [7], m = 8 Output:,5,0,7,5 [6]Copy the code

Note:

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

parsing

SQL > select * from ‘queries’ where’ queries’ [I] = ‘r’; SQL > select * from ‘queries’ where’ queries’ [I] = ‘r’;

answer

class Solution(object):
    def processQueries(self, queries, m):
        """
        :type queries: List[int]
        :type m: int
        :rtype: List[int]
        """
        p = [i+1 for i in range(m)]
        r = []
        for i in queries:
            r.append(p.index(i))
            p.remove(i)
            p = [i] + p
        return r    	      
		
Copy the code

The results

Each node in the linked list has the same mutation as each node in the linked list. Each node in the Python online list has the same value as each node in the list.Copy the code

Original link: leetcode.com/problems/qu…

Your support is my biggest motivation