Skip to content

Commit ff70a13

Browse files
committed
Add solution #1409
1 parent 1d03392 commit ff70a13

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,286 LeetCode solutions in JavaScript
1+
# 1,287 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1076,6 +1076,7 @@
10761076
1405|[Longest Happy String](./solutions/1405-longest-happy-string.js)|Medium|
10771077
1406|[Stone Game III](./solutions/1406-stone-game-iii.js)|Hard|
10781078
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|
1079+
1409|[Queries on a Permutation With Key](./solutions/1409-queries-on-a-permutation-with-key.js)|Medium|
10791080
1410|[HTML Entity Parser](./solutions/1410-html-entity-parser.js)|Medium|
10801081
1415|[The k-th Lexicographical String of All Happy Strings of Length n](./solutions/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js)|Medium|
10811082
1422|[Maximum Score After Splitting a String](./solutions/1422-maximum-score-after-splitting-a-string.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 1409. Queries on a Permutation With Key
3+
* https://leetcode.com/problems/queries-on-a-permutation-with-key/
4+
* Difficulty: Medium
5+
*
6+
* Given the array queries of positive integers between 1 and m, you have to process all
7+
* queries[i] (from i=0 to i=queries.length-1) according to the following rules:
8+
* - In the beginning, you have the permutation P=[1,2,3,...,m].
9+
* - For the current i, find the position of queries[i] in the permutation P (indexing from 0)
10+
* and then move this at the beginning of the permutation P. Notice that the position of
11+
* queries[i] in P is the result for queries[i].
12+
*
13+
* Return an array containing the result for the given queries.
14+
*/
15+
16+
/**
17+
* @param {number[]} queries
18+
* @param {number} m
19+
* @return {number[]}
20+
*/
21+
var processQueries = function(queries, m) {
22+
const permutation = Array.from({ length: m }, (_, i) => i + 1);
23+
const result = [];
24+
25+
for (const query of queries) {
26+
const position = permutation.indexOf(query);
27+
result.push(position);
28+
permutation.splice(position, 1);
29+
permutation.unshift(query);
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)