1409. Queries on a Permutation With Key Medium
1/**
2 * [1409] Queries on a Permutation With Key
3 *
4 * 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:
5 *
6 *
7 * In the beginning, you have the permutation P=[1,2,3,...,m].
8 * 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].
9 *
10 *
11 * Return an array containing the result for the given queries.
12 *
13 *
14 * Example 1:
15 *
16 *
17 * Input: queries = [3,1,2,1], m = 5
18 * Output: [2,1,2,1]
19 * Explanation: The queries are processed as follow:
20 * 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].
21 * 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].
22 * 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].
23 * 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].
24 * Therefore, the array containing the result is [2,1,2,1].
25 *
26 *
27 * Example 2:
28 *
29 *
30 * Input: queries = [4,1,2,2], m = 4
31 * Output: [3,1,2,0]
32 *
33 *
34 * Example 3:
35 *
36 *
37 * Input: queries = [7,5,5,8,3], m = 8
38 * Output: [6,5,0,7,5]
39 *
40 *
41 *
42 * Constraints:
43 *
44 *
45 * 1 <= m <= 10^3
46 * 1 <= queries.length <= m
47 * 1 <= queries[i] <= m
48 *
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/queries-on-a-permutation-with-key/
53// discuss: https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58 pub fn process_queries(queries: Vec<i32>, m: i32) -> Vec<i32> {
59 vec![]
60 }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn test_1409() {
71 }
72}
73
Back
© 2025 bowen.ge All Rights Reserved.