Skip to content

Commit

Permalink
Sync LeetCode submission Runtime - 6 ms (39.07%), Memory - 3.2 MB (33…
Browse files Browse the repository at this point in the history
….11%)
  • Loading branch information
nathannaveen committed May 1, 2024
1 parent 15d52b2 commit d70a301
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 3347-distribute-elements-into-two-arrays-i/README.md
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>&nbsp;</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 &gt; 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 &gt; 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 &gt; 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>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>3 &lt;= n &lt;= 50</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
<li>All elements in <code>nums</code> are distinct.</li>
</ul>
14 changes: 14 additions & 0 deletions 3347-distribute-elements-into-two-arrays-i/solution.go
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...)
}

0 comments on commit d70a301

Please sign in to comment.