-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 6 ms (39.07%), Memory - 3.2 MB (33…
….11%)
- Loading branch information
1 parent
15d52b2
commit d70a301
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
func resultArray(nums []int) []int { | ||
arr1 := []int{nums[0]} | ||
arr2 := []int{nums[1]} | ||
|
||
for i := 2; i < len(nums); i++ { | ||
if arr1[len(arr1) - 1] > arr2[len(arr2) - 1] { | ||
arr1 = append(arr1, nums[i]) | ||
} else { | ||
arr2 = append(arr2, nums[i]) | ||
} | ||
} | ||
|
||
return append(arr1, arr2...) | ||
} |