Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ad6ee4

Browse files
committed
feat(1409): ✨整理1409解题代码
1 parent dc3458a commit 2ad6ee4

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Rank/184/1409/solution1.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* https://leetcode-cn.com/problems/queries-on-a-permutation-with-key/
3+
*
4+
* 1409. 查询带键的排列
5+
*
6+
* Medium
7+
*
8+
* 76ms
9+
* 41mb
10+
*
11+
* 时间复杂度 O(n^2)
12+
* 空间复杂度 O(n)
13+
*/
14+
const processQueries = (queries, m) => {
15+
const ans = [];
16+
const list = Array.from({ length: m }, (item, index) => index + 1);
17+
18+
for (let i = 0, max = queries.length; i < max; i++) {
19+
const item = queries[i];
20+
const index = list.findIndex(el => el === item);
21+
ans.push(index);
22+
list.splice(index, 1);
23+
list.unshift(item);
24+
}
25+
return ans;
26+
}

0 commit comments

Comments
 (0)