mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
45 lines
2.4 KiB
HTML
45 lines
2.4 KiB
HTML
<p>You are given a <strong>1-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code> of length <code>n</code>.</p>
|
|
|
|
<p>You need to distribute all the elements of <code>nums</code> between two arrays <code>arr1</code> and <code>arr2</code> using <code>n</code> operations. In the first operation, append <code>nums[1]</code> to <code>arr1</code>. In the second operation, append <code>nums[2]</code> to <code>arr2</code>. Afterwards, in the <code>i<sup>th</sup></code> operation:</p>
|
|
|
|
<ul>
|
|
<li>If the last element of <code>arr1</code> is<strong> greater</strong> than the last element of <code>arr2</code>, append <code>nums[i]</code> to <code>arr1</code>. Otherwise, append <code>nums[i]</code> to <code>arr2</code>.</li>
|
|
</ul>
|
|
|
|
<p>The array <code>result</code> is formed by concatenating the arrays <code>arr1</code> and <code>arr2</code>. For example, if <code>arr1 == [1,2,3]</code> and <code>arr2 == [4,5,6]</code>, then <code>result = [1,2,3,4,5,6]</code>.</p>
|
|
|
|
<p>Return <em>the array</em> <code>result</code>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [2,1,3]
|
|
<strong>Output:</strong> [2,3,1]
|
|
<strong>Explanation:</strong> After the first 2 operations, arr1 = [2] and arr2 = [1].
|
|
In the 3<sup>rd</sup> operation, as the last element of arr1 is greater than the last element of arr2 (2 > 1), append nums[3] to arr1.
|
|
After 3 operations, arr1 = [2,3] and arr2 = [1].
|
|
Hence, the array result formed by concatenation is [2,3,1].
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [5,4,3,8]
|
|
<strong>Output:</strong> [5,3,4,8]
|
|
<strong>Explanation:</strong> After the first 2 operations, arr1 = [5] and arr2 = [4].
|
|
In the 3<sup>rd</sup> operation, as the last element of arr1 is greater than the last element of arr2 (5 > 4), append nums[3] to arr1, hence arr1 becomes [5,3].
|
|
In the 4<sup>th</sup> operation, as the last element of arr2 is greater than the last element of arr1 (4 > 3), append nums[4] to arr2, hence arr2 becomes [4,8].
|
|
After 4 operations, arr1 = [5,3] and arr2 = [4,8].
|
|
Hence, the array result formed by concatenation is [5,3,4,8].
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>3 <= n <= 50</code></li>
|
|
<li><code>1 <= nums[i] <= 100</code></li>
|
|
<li>All elements in <code>nums</code> are distinct.</li>
|
|
</ul>
|