-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path3437-permutations-iii.js
More file actions
48 lines (41 loc) · 1.13 KB
/
3437-permutations-iii.js
File metadata and controls
48 lines (41 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 3437. Permutations III
* https://leetcode.com/problems/permutations-iii/
* Difficulty: Medium
*
* Given an integer n, an alternating permutation is a permutation of the first n positive
* integers such that no two adjacent elements are both odd or both even.
*
* Return all such alternating permutations sorted in lexicographical order.
*/
/**
* @param {number} n
* @return {number[][]}
*/
var permute = function(n) {
const result = [];
const current = [];
const used = new Array(n + 1).fill(false);
backtrack(current, used, result, n);
return result;
function backtrack(current, used, result, n) {
if (current.length === n) {
result.push([...current]);
return;
}
for (let num = 1; num <= n; num++) {
if (used[num]) continue;
if (current.length > 0) {
const lastNum = current[current.length - 1];
if ((lastNum % 2 === 0 && num % 2 === 0) || (lastNum % 2 === 1 && num % 2 === 1)) {
continue;
}
}
current.push(num);
used[num] = true;
backtrack(current, used, result, n);
current.pop();
used[num] = false;
}
}
};