Class Solution
java.lang.Object
g1701_1800.s1718_construct_the_lexicographically_largest_valid_sequence.Solution
1718 - Construct the Lexicographically Largest Valid Sequence.<p>Medium</p>
<p>Given an integer <code>n</code>, find a sequence that satisfies all of the following:</p>
<ul>
<li>The integer <code>1</code> occurs once in the sequence.</li>
<li>Each integer between <code>2</code> and <code>n</code> occurs twice in the sequence.</li>
<li>For every integer <code>i</code> between <code>2</code> and <code>n</code>, the <strong>distance</strong> between the two occurrences of <code>i</code> is exactly <code>i</code>.</li>
</ul>
<p>The <strong>distance</strong> between two numbers on the sequence, <code>a[i]</code> and <code>a[j]</code>, is the absolute difference of their indices, <code>|j - i|</code>.</p>
<p>Return <em>the <strong>lexicographically largest</strong> sequence</em>_. It is guaranteed that under the given constraints, there is always a solution._</p>
<p>A sequence <code>a</code> is lexicographically larger than a sequence <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, sequence <code>a</code> has a number greater than the corresponding number in <code>b</code>. For example, <code>[0,1,9,0]</code> is lexicographically larger than <code>[0,1,5,6]</code> because the first position they differ is at the third number, and <code>9</code> is greater than <code>5</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> n = 3</p>
<p><strong>Output:</strong> [3,1,2,3,2]</p>
<p><strong>Explanation:</strong> [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> n = 5</p>
<p><strong>Output:</strong> [5,3,1,4,3,5,2,4,2]</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 20</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
constructDistancedSequence
public int[] constructDistancedSequence(int n)
-