Skip to content

Commit 1545a27

Browse files
committed
Add solution #1718
1 parent ca5cf4b commit 1545a27

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@
492492
1672|[Richest Customer Wealth](./1672-richest-customer-wealth.js)|Easy|
493493
1679|[Max Number of K-Sum Pairs](./1679-max-number-of-k-sum-pairs.js)|Medium|
494494
1716|[Calculate Money in Leetcode Bank](./1716-calculate-money-in-leetcode-bank.js)|Easy|
495+
1718|[Construct the Lexicographically Largest Valid Sequence](./1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
495496
1726|[Tuple with Same Product](./1726-tuple-with-same-product.js)|Medium|
496497
1732|[Find the Highest Altitude](./1732-find-the-highest-altitude.js)|Easy|
497498
1748|[Sum of Unique Elements](./1748-sum-of-unique-elements.js)|Easy|
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* 1718. Construct the Lexicographically Largest Valid Sequence
3+
* https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer n, find a sequence that satisfies all of the following:
7+
* - The integer 1 occurs once in the sequence.
8+
* - Each integer between 2 and n occurs twice in the sequence.
9+
* - For every integer i between 2 and n, the distance between the two occurrences of i is
10+
* exactly i.
11+
*
12+
* The distance between two numbers on the sequence, a[i] and a[j], is the absolute difference
13+
* of their indices, |j - i|.
14+
*
15+
* Return the lexicographically largest sequence. It is guaranteed that under the given
16+
* constraints, there is always a solution.
17+
*
18+
* A sequence a is lexicographically larger than a sequence b (of the same length) if in the first
19+
* position where a and b differ, sequence a has a number greater than the corresponding number in
20+
* b. For example, [0,1,9,0] is lexicographically larger than [0,1,5,6] because the first position
21+
* they differ is at the third number, and 9 is greater than 5.
22+
*/
23+
24+
/**
25+
* @param {number} n
26+
* @return {number[]}
27+
*/
28+
var constructDistancedSequence = function(n) {
29+
const result = new Array(2 * n - 1).fill(0);
30+
const group = new Array(n + 1).fill(false);
31+
32+
backtrack(0);
33+
34+
function backtrack(index) {
35+
if (index === 2 * n - 1) {
36+
return true;
37+
} else if (result[index] !== 0) {
38+
return backtrack(index + 1);
39+
}
40+
for (let num = n; num >= 1; num--) {
41+
if (group[num]) {
42+
continue;
43+
}
44+
group[num] = true;
45+
result[index] = num;
46+
if (num === 1 || (index + num < 2 * n - 1 && result[index + num] === 0)) {
47+
if (num > 1) {
48+
result[index + num] = num;
49+
}
50+
if (backtrack(index + 1)) {
51+
return true;
52+
}
53+
if (num > 1) {
54+
result[index + num] = 0;
55+
}
56+
}
57+
result[index] = 0;
58+
group[num] = false;
59+
}
60+
return false;
61+
}
62+
63+
return result;
64+
};

0 commit comments

Comments
 (0)