-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructDistancedSequence.js
More file actions
40 lines (37 loc) · 958 Bytes
/
constructDistancedSequence.js
File metadata and controls
40 lines (37 loc) · 958 Bytes
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
/**
* @param {number} n
* @return {number[]}
*/
var constructDistancedSequence = function (n) {
let result = Array(2 * n - 1).fill(0);
let used = Array(n + 1).fill(false);
backtrack(result, used, n, 0);
return result;
};
function backtrack(result, used, n, index) {
while (index < result.length && result[index] !== 0) {
index++;
}
if (index === result.length) {
return true;
}
for (let i = n; i >= 1; i--) {
if (used[i]) continue;
if (i === 1) {
result[index] = 1;
used[1] = true;
if (backtrack(result, used, n, index + 1)) return true;
result[index] = 0;
used[1] = false;
} else if (index + i < result.length && result[index + i] === 0) {
result[index] = i;
result[index + i] = i;
used[i] = true;
if (backtrack(result, used, n, index + 1)) return true;
result[index] = 0;
result[index + i] = 0;
used[i] = false;
}
}
return false;
}