-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDistributeElementsIntoTwoArraysI.java
37 lines (31 loc) · 1.09 KB
/
DistributeElementsIntoTwoArraysI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// https://leetcode.com/problems/distribute-elements-into-two-arrays-i
// T: O(N)
// S: O(N)
import java.util.ArrayList;
import java.util.List;
public class DistributeElementsIntoTwoArraysI {
public int[] resultArray(int[] array) {
final List<Integer> array1 = new ArrayList<>();
final List<Integer> array2 = new ArrayList<>();
array1.add(array[0]);
array2.add(array[1]);
for (int i = 2 ; i < array.length ; i++) {
if (array1.get(array1.size() - 1) > array2.get(array2.size() - 1)) {
array1.add(array[i]);
} else {
array2.add(array[i]);
}
}
return concatenate(array1, array2);
}
private static int[] concatenate(List<Integer> array1, List<Integer> array2) {
final int[] result = new int[array1.size() + array2.size()];
for (int i = 0 ; i < array1.size() ; i++) {
result[i] = array1.get(i);
}
for (int i = 0 ; i < array2.size() ; i++) {
result[array1.size() + i] = array2.get(i);
}
return result;
}
}