1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/构建字典序最大的可行序列 [construct-the-lexicographically-largest-valid-sequence].html
2022-03-29 12:43:11 +08:00

37 lines
1.6 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个整数 <code>n</code> ,请你找到满足下面条件的一个序列:</p>
<ul>
<li>整数 <code>1</code> 在序列中只出现一次。</li>
<li><code>2</code> 到 <code>n</code> 之间每个整数都恰好出现两次。</li>
<li>对于每个 <code>2</code> 到 <code>n</code> 之间的整数 <code>i</code> ,两个 <code>i</code> 之间出现的距离恰好为 <code>i</code> 。</li>
</ul>
<p>序列里面两个数 <code>a[i]</code> 和 <code>a[j]</code> 之间的 <strong>距离</strong> ,我们定义为它们下标绝对值之差 <code>|j - i|</code> 。</p>
<p>请你返回满足上述条件中 <strong>字典序最大</strong> 的序列。题目保证在给定限制条件下,一定存在解。</p>
<p>一个序列 <code>a</code> 被认为比序列 <code>b</code> (两者长度相同)字典序更大的条件是: <code>a</code> 和 <code>b</code> 中第一个不一样的数字处,<code>a</code> 序列的数字比 <code>b</code> 序列的数字大。比方说,<code>[0,1,9,0]</code> 比 <code>[0,1,5,6]</code> 字典序更大,因为第一个不同的位置是第三个数字,且 <code>9</code> 比 <code>5</code> 大。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>n = 3
<b>输出:</b>[3,1,2,3,2]
<b>解释:</b>[2,3,2,1,3] 也是一个可行的序列,但是 [3,1,2,3,2] 是字典序最大的序列。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>n = 5
<b>输出:</b>[5,3,1,4,3,5,2,4,2]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 20</code></li>
</ul>