Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7cd6a7b

Browse files
committed
Add solution #3069
1 parent e3b44e8 commit 7cd6a7b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,7 @@
21542154
3066|[Minimum Operations to Exceed Threshold Value II](./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js)|Medium|
21552155
3067|[Count Pairs of Connectable Servers in a Weighted Tree Network](./solutions/3067-count-pairs-of-connectable-servers-in-a-weighted-tree-network.js)|Medium|
21562156
3068|[Find the Maximum Sum of Node Values](./solutions/3068-find-the-maximum-sum-of-node-values.js)|Hard|
2157+
3069|[Distribute Elements Into Two Arrays I](./solutions/3069-distribute-elements-into-two-arrays-i.js)|Easy|
21572158
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./solutions/3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
21582159
3108|[Minimum Cost Walk in Weighted Graph](./solutions/3108-minimum-cost-walk-in-weighted-graph.js)|Hard|
21592160
3110|[Score of a String](./solutions/3110-score-of-a-string.js)|Easy|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 3069. Distribute Elements Into Two Arrays I
3+
* https://leetcode.com/problems/distribute-elements-into-two-arrays-i/
4+
* Difficulty: Easy
5+
*
6+
* You are given a 1-indexed array of distinct integers nums of length n.
7+
*
8+
* You need to distribute all the elements of nums between two arrays arr1 and arr2 using n
9+
* operations. In the first operation, append nums[1] to arr1. In the second operation,
10+
* append nums[2] to arr2. Afterwards, in the ith operation:
11+
* - If the last element of arr1 is greater than the last element of arr2, append nums[i] to arr1.
12+
* Otherwise, append nums[i] to arr2.
13+
*
14+
* The array result is formed by concatenating the arrays arr1 and arr2. For example, if
15+
* arr1 == [1,2,3] and arr2 == [4,5,6], then result = [1,2,3,4,5,6].
16+
*
17+
* Return the array result.
18+
*/
19+
20+
/**
21+
* @param {number[]} nums
22+
* @return {number[]}
23+
*/
24+
var resultArray = function(nums) {
25+
const arr1 = [nums[0]];
26+
const arr2 = [nums[1]];
27+
28+
for (let i = 2; i < nums.length; i++) {
29+
if (arr1[arr1.length - 1] > arr2[arr2.length - 1]) {
30+
arr1.push(nums[i]);
31+
} else {
32+
arr2.push(nums[i]);
33+
}
34+
}
35+
36+
return [...arr1, ...arr2];
37+
};

0 commit comments

Comments
 (0)