class Solution:
def processQueries(self, queries, m):
P = list(range(1, m+1))
res = []
for q in queries:
idx = P.index(q)
res.append(idx)
P.pop(idx)
P.insert(0, q)
return res
class Solution {
public:
vector<int> processQueries(vector<int>& queries, int m) {
vector<int> P(m);
iota(P.begin(), P.end(), 1);
vector<int> res;
for (int q : queries) {
auto it = find(P.begin(), P.end(), q);
int idx = it - P.begin();
res.push_back(idx);
P.erase(it);
P.insert(P.begin(), q);
}
return res;
}
};
class Solution {
public int[] processQueries(int[] queries, int m) {
List<Integer> P = new ArrayList<>();
for (int i = 1; i <= m; i++) P.add(i);
int[] res = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int idx = P.indexOf(queries[i]);
res[i] = idx;
P.remove(idx);
P.add(0, queries[i]);
}
return res;
}
}
var processQueries = function(queries, m) {
let P = [];
for (let i = 1; i <= m; i++) P.push(i);
let res = [];
for (let q of queries) {
let idx = P.indexOf(q);
res.push(idx);
P.splice(idx, 1);
P.unshift(q);
}
return res;
};
You are given an array of integers queries
and an integer m
. You must process each element in queries
as follows:
P
of the integers from 1 to m
(i.e., P = [1, 2, ..., m]
).q
in queries
:
q
in P
(0-based).q
to the front of P
(so the order of other elements is preserved).Return the resulting array of indices. Each query is processed in order, and the permutation is updated after each query.
Constraints:
m
≤ 103queries.length
≤ 104queries[i]
≤ m
The core of this problem is simulating a dynamic array where, for each query, you locate an element, record its position, and then move it to the front.
The most straightforward approach is to use a list to represent the permutation and for each query:
m
or many queries, since each operation is O(m).
To optimize, you might consider data structures that allow faster lookups and moves, such as a linked list or a segment tree with position tracking. However, given the constraints (with m
up to 1000), the naive approach is actually fast enough.
The key realization is that the problem is about simulating the process accurately, not about finding a mathematically optimal answer.
Let's break down the steps to solve the problem:
P
containing numbers from 1 to m
.q
in queries
:q
in P
(using indexOf
or equivalent).q
from its current position.q
at the front (index 0) of P
.Each step is justified because:
P
and update it after every query.
Let's consider queries = [3,1,2,1]
and m = 5
.
P = [1,2,3,4,5]
3
:
P[2] = 3
)P = [3,1,2,4,5]
1
:
P[1] = 1
)P = [1,3,2,4,5]
2
:
P[2] = 2
)P = [2,1,3,4,5]
1
:
P[1] = 1
)P = [1,2,3,4,5]
The final result is [2, 1, 2, 1]
.
Brute-force approach:
n
queries, total time is O(n * m).Optimized approaches:
m
up to 1000 and n
up to 10,000, the brute-force approach is efficient enough.
This problem is a simulation of a dynamic array where queried elements are moved to the front after each operation. While there are more advanced ways to optimize for larger m
, the constraints allow a direct and intuitive approach using simple list operations. The key insight is that maintaining and updating the permutation after each query is straightforward and efficient for the input sizes given.