1718. Construct the Lexicographically Largest Valid Sequence #1322
-
Topics: Given an integer
The distance between two numbers on the sequence, Return the lexicographically largest sequence. It is guaranteed that under the given constraints, there is always a solution. A sequence Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to construct the lexicographically largest valid sequence that satisfies specific constraints. The sequence must include the integer 1 once and each integer from 2 to n exactly twice, with the distance between their occurrences equal to their value. ApproachThe approach uses a backtracking algorithm to build the sequence step-by-step, ensuring that each number is placed in the highest possible position to achieve the lexicographically largest sequence. The key steps are:
Let's implement this solution in PHP: 1718. Construct the Lexicographically Largest Valid Sequence <?php
/**
* @param Integer $n
* @return Integer[]
*/
function constructDistancedSequence($n) {
$length = 2 * $n - 1;
$sequence = array_fill(0, $length, 0);
$used = array_fill(0, $n + 1, 0); // used[0] unused
$reserved = array_fill(0, $length, null);
$success = backtrack(0, $sequence, $used, $reserved, $n);
return $success ? $sequence : [];
}
/**
* @param $current_pos
* @param $sequence
* @param $used
* @param $reserved
* @param $n
* @return bool
*/
function backtrack($current_pos, &$sequence, &$used, &$reserved, $n) {
$length = 2 * $n - 1;
if ($current_pos >= $length) {
return true;
}
if ($sequence[$current_pos] != 0) {
return backtrack($current_pos + 1, $sequence, $used, $reserved, $n);
}
if ($reserved[$current_pos] !== null) {
$num = $reserved[$current_pos];
if ($used[$num] != 1) {
return false;
}
// Place the second occurrence
$sequence[$current_pos] = $num;
$used[$num] = 2;
$success = backtrack($current_pos + 1, $sequence, $used, $reserved, $n);
if ($success) {
return true;
} else {
$sequence[$current_pos] = 0;
$used[$num] = 1;
return false;
}
} else {
// Try placing numbers from n down to 1
for ($num = $n; $num >= 1; $num--) {
if ($num == 1) {
if ($used[1] == 0) {
$sequence[$current_pos] = 1;
$used[1] = 1;
$success = backtrack($current_pos + 1, $sequence, $used, $reserved, $n);
if ($success) {
return true;
}
$sequence[$current_pos] = 0;
$used[1] = 0;
}
} else {
if ($used[$num] == 0) {
$next_pos = $current_pos + $num;
if ($next_pos < $length && $sequence[$next_pos] == 0 && $reserved[$next_pos] === null) {
$sequence[$current_pos] = $num;
$used[$num] = 1;
$reserved[$next_pos] = $num;
$success = backtrack($current_pos + 1, $sequence, $used, $reserved, $n);
if ($success) {
return true;
}
$sequence[$current_pos] = 0;
$used[$num] = 0;
$reserved[$next_pos] = null;
}
}
}
}
return false;
}
}
// Test cases
print_r(constructDistancedSequence(3));
print_r(constructDistancedSequence(5));
?> Explanation:
This approach efficiently explores possible sequences while ensuring the lexicographically largest valid sequence is found first. |
Beta Was this translation helpful? Give feedback.
We need to construct the lexicographically largest valid sequence that satisfies specific constraints. The sequence must include the integer 1 once and each integer from 2 to n exactly twice, with the distance between their occurrences equal to their value.
Approach
The approach uses a backtracking algorithm to build the sequence step-by-step, ensuring that each number is placed in the highest possible position to achieve the lexicographically largest sequence. The key steps are: