|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../string-matching-in-an-array "String Matching in an Array") |
| 9 | + |
| 10 | +[Next >](../html-entity-parser "HTML Entity Parser") |
| 11 | + |
| 12 | +## [1409. Queries on a Permutation With Key (Medium)](https://leetcode.com/problems/queries-on-a-permutation-with-key "查询带键的排列") |
| 13 | + |
| 14 | +<p>Given the array <code>queries</code> of positive integers between <code>1</code> and <code>m</code>, you have to process all <code>queries[i]</code> (from <code>i=0</code> to <code>i=queries.length-1</code>) according to the following rules:</p> |
| 15 | + |
| 16 | +<ul> |
| 17 | + <li>In the beginning, you have the permutation <code>P=[1,2,3,...,m]</code>.</li> |
| 18 | + <li>For the current <code>i</code>, find the position of <code>queries[i]</code> in the permutation <code>P</code> (<strong>indexing from 0</strong>) and then move this at the beginning of the permutation <code>P.</code> Notice that the position of <code>queries[i]</code> in <code>P</code> is the result for <code>queries[i]</code>.</li> |
| 19 | +</ul> |
| 20 | + |
| 21 | +<p>Return an array containing the result for the given <code>queries</code>.</p> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | +<p><strong>Example 1:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> queries = [3,1,2,1], m = 5 |
| 28 | +<strong>Output:</strong> [2,1,2,1] |
| 29 | +<strong>Explanation:</strong> The queries are processed as follow: |
| 30 | +For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is <strong>2</strong>, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. |
| 31 | +For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is <strong>1</strong>, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. |
| 32 | +For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is <strong>2</strong>, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. |
| 33 | +For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is <strong>1</strong>, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. |
| 34 | +Therefore, the array containing the result is [2,1,2,1]. |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p><strong>Example 2:</strong></p> |
| 38 | + |
| 39 | +<pre> |
| 40 | +<strong>Input:</strong> queries = [4,1,2,2], m = 4 |
| 41 | +<strong>Output:</strong> [3,1,2,0] |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p><strong>Example 3:</strong></p> |
| 45 | + |
| 46 | +<pre> |
| 47 | +<strong>Input:</strong> queries = [7,5,5,8,3], m = 8 |
| 48 | +<strong>Output:</strong> [6,5,0,7,5] |
| 49 | +</pre> |
| 50 | + |
| 51 | +<p> </p> |
| 52 | +<p><strong>Constraints:</strong></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li><code>1 <= m <= 10^3</code></li> |
| 56 | + <li><code>1 <= queries.length <= m</code></li> |
| 57 | + <li><code>1 <= queries[i] <= m</code></li> |
| 58 | +</ul> |
| 59 | + |
| 60 | +### Related Topics |
| 61 | + [[Array](../../tag/array/README.md)] |
| 62 | + |
| 63 | +### Hints |
| 64 | +<details> |
| 65 | +<summary>Hint 1</summary> |
| 66 | +Create the permutation P=[1,2,...,m], it could be a list for example. |
| 67 | +</details> |
| 68 | + |
| 69 | +<details> |
| 70 | +<summary>Hint 2</summary> |
| 71 | +For each i, find the position of queries[i] with a simple scan over P and then move this to the beginning. |
| 72 | +</details> |
0 commit comments