Formatted question description: https://leetcode.ca/all/1409.html

1409. Queries on a Permutation With Key (Medium)

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]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. 
For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. 
For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. 
For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. 
Therefore, the array containing the result is [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 Topics:
Array

Solution 1. Brute force

Simulate the process.

// OJ: https://leetcode.com/problems/queries-on-a-permutation-with-key/
// Time: O(MN)
// Space: O(M)
class Solution {
public:
    vector<int> processQueries(vector<int>& Q, int m) {
        int N = Q.size();
        vector<int> v(m), ans;
        for (int i = 0; i < m; ++i) v[i] = i+1;
        for (int q : Q) {
            int i = 0;
            for (; i < m && v[i] != q; ++i);
            ans.push_back(i);
            for (int j = i; j > 0; --j) v[j] = v[j - 1];
            v[0] = q;
        }
        return ans;
    }
};

Java

  • class Solution {
        public int[] processQueries(int[] queries, int m) {
            LinkedList<Integer> list = new LinkedList<Integer>();
            for (int i = 1; i <= m; i++)
                list.add(i);
            int length = queries.length;
            int[] queryResults = new int[length];
            for (int i = 0; i < length; i++) {
                int query = queries[i];
                int index = list.indexOf(query);
                queryResults[i] = index;
                list.remove(new Integer(query));
                list.addFirst(query);
            }
            return queryResults;
        }
    }
    
  • // OJ: https://leetcode.com/problems/queries-on-a-permutation-with-key/
    // Time: O(MN)
    // Space: O(M)
    class Solution {
    public:
        vector<int> processQueries(vector<int>& Q, int m) {
            int N = Q.size();
            vector<int> v(m), ans;
            for (int i = 0; i < m; ++i) v[i] = i+1;
            for (int q : Q) {
                int i = 0;
                for (; i < m && v[i] != q; ++i);
                ans.push_back(i);
                for (int j = i; j > 0; --j) v[j] = v[j - 1];
                v[0] = q;
            }
            return ans;
        }
    };
    
  • class Solution:
        def processQueries(self, queries: List[int], m: int) -> List[int]:
            P = list(range(1, m + 1))
            res = []
            for q in queries:
                idx = P.index(q)
                P = [q] + P[:idx] + P[idx+1:]
                res.append(idx)
            return res
    

All Problems

All Solutions