Class Solution
-
- All Implemented Interfaces:
public final class Solution1718 - Construct the Lexicographically Largest Valid Sequence\.
Medium
Given an integer
n, find a sequence that satisfies all of the following:The integer
1occurs once in the sequence.Each integer between
2andnoccurs twice in the sequence.For every integer
ibetween2andn, the distance between the two occurrences ofiis exactlyi.
The distance between two numbers on the sequence,
a[i]anda[j], is the absolute difference of their indices,|j - i|.Return the lexicographically largest sequence_. It is guaranteed that under the given constraints, there is always a solution._
A sequence
ais lexicographically larger than a sequenceb(of the same length) if in the first position whereaandbdiffer, sequenceahas a number greater than the corresponding number inb. For example,[0,1,9,0]is lexicographically larger than[0,1,5,6]because the first position they differ is at the third number, and9is greater than5.Example 1:
Input: n = 3
Output: 3,1,2,3,2
Explanation: 2,3,2,1,3 is also a valid sequence, but 3,1,2,3,2 is the lexicographically largest valid sequence.
Example 2:
Input: n = 5
Output: 5,3,1,4,3,5,2,4,2
Constraints:
1 <= n <= 20
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntArrayconstructDistancedSequence(Integer n)-
-
Method Detail
-
constructDistancedSequence
final IntArray constructDistancedSequence(Integer n)
-
-
-
-