1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-12-21 03:13:45 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (English)/字典序最小和为目标值且绝对值是排列的数组(English) [lexicographically-smallest-negated-permutation-that-sums-to-target].html
2025-12-06 16:04:11 +08:00

63 lines
2.1 KiB
HTML

<p>You are given a positive integer <code>n</code> and an integer <code>target</code>.</p>
<p>Return the <strong><span data-keyword="lexicographically-smaller-array">lexicographically smallest</span></strong> array of integers of size <code>n</code> such that:</p>
<ul>
<li>The <strong>sum</strong> of its elements equals <code>target</code>.</li>
<li>The <strong>absolute values</strong> of its elements form a <strong>permutation</strong> of size <code>n</code>.</li>
</ul>
<p>If no such array exists, return an empty array.</p>
<p>A <strong>permutation</strong> of size <code>n</code> is a rearrangement of integers <code>1, 2, ..., n</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, target = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">[-3,1,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>The arrays that sum to 0 and whose absolute values form a permutation of size 3 are:</p>
<ul>
<li><code>[-3, 1, 2]</code></li>
<li><code>[-3, 2, 1]</code></li>
<li><code>[-2, -1, 3]</code></li>
<li><code>[-2, 3, -1]</code></li>
<li><code>[-1, -2, 3]</code></li>
<li><code>[-1, 3, -2]</code></li>
<li><code>[1, -3, 2]</code></li>
<li><code>[1, 2, -3]</code></li>
<li><code>[2, -3, 1]</code></li>
<li><code>[2, 1, -3]</code></li>
<li><code>[3, -2, -1]</code></li>
<li><code>[3, -1, -2]</code></li>
</ul>
<p>The lexicographically smallest one is <code>[-3, 1, 2]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, target = 10000000000</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no arrays that sum to <span class="example-io">10000000000 and whose absolute values form a permutation of size 1. Therefore, the answer is <code>[]</code>.</span></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>10</sup> &lt;= target &lt;= 10<sup>10</sup></code></li>
</ul>