You are given an integer array nums.
You must split it into two arrays list1 and list2 such that:
list1 starts with nums[0]list2 starts with nums[1]i from 2 to n-1, compare the last elements of list1 and list2:
list1 > last element of list2, put nums[i] in list1nums[i] in list2Finally, return the concatenation of list1 followed by list2.
2 <= nums.length <= 1001 <= nums[i] <= 100Use two ArrayLists to simulate the distribution process with comparison-based placement.
Algorithm:
list1 with nums[0] and list2 with nums[1]list1 with last element of list2list1.last > list2.last, add to list1list2list1 followed by list2This approach works because:
Example 1: nums = [2, 1, 3, 3]
Step-by-step execution:
Result: [2, 3, 3, 1]
Example 2: nums = [5, 4, 3, 8]
Step-by-step execution:
Result: [5, 3, 4, 8]
Example 3: nums = [1, 3]
Step-by-step execution:
Result: [1, 3]
class Solution {
public int[] resultArray(int[] nums) {
int length = nums.length;
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
list1.add(nums[0]);
list2.add(nums[1]);
for (int i = 2; i < length; i++) {
int lastList1 = list1.get(list1.size() - 1);
int lastList2 = list2.get(list2.size() - 1);
if (lastList1 > lastList2) {
list1.add(nums[i]);
} else {
list2.add(nums[i]);
}
}
int[] ans = new int[length];
int index = 0;
for (int ele : list1) {
ans[index++] = ele;
}
for (int ele : list2) {
ans[index++] = ele;
}
return ans;
}
}
class Solution {
public int[] resultArray(int[] nums) {
int n = nums.length;
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
list1.add(nums[0]);
list2.add(nums[1]);
for (int i = 2; i < n; i++) {
if (list1.get(list1.size() - 1) > list2.get(list2.size() - 1)) {
list1.add(nums[i]);
} else {
list2.add(nums[i]);
}
}
// Build result array
int[] result = new int[n];
int idx = 0;
for (int num : list1) result[idx++] = num;
for (int num : list2) result[idx++] = num;
return result;
}
}
Explanation: Same logic but with more explicit variable naming.
Time Complexity: O(n) - same as main approach Space Complexity: O(n) - same as main approach
class Solution {
public int[] resultArray(int[] nums) {
int n = nums.length;
List<Integer> list1 = new ArrayList<>(n);
List<Integer> list2 = new ArrayList<>(n);
list1.add(nums[0]);
list2.add(nums[1]);
for (int i = 2; i < n; i++) {
int last1 = list1.get(list1.size() - 1);
int last2 = list2.get(list2.size() - 1);
if (last1 > last2) {
list1.add(nums[i]);
} else {
list2.add(nums[i]);
}
}
int[] result = new int[n];
for (int i = 0; i < list1.size(); i++) {
result[i] = list1.get(i);
}
for (int i = 0; i < list2.size(); i++) {
result[list1.size() + i] = list2.get(i);
}
return result;
}
}
Explanation: Pre-size ArrayLists and use indexed assignment for result.
Time Complexity: O(n) - same performance Space Complexity: O(n) - same as main approach
class Solution {
public int[] resultArray(int[] nums) {
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
list1.add(nums[0]);
list2.add(nums[1]);
for (int i = 2; i < nums.length; i++) {
int last1 = list1.get(list1.size() - 1);
int last2 = list2.get(list2.size() - 1);
if (last1 > last2) {
list1.add(nums[i]);
} else {
list2.add(nums[i]);
}
}
return Stream.concat(list1.stream(), list2.stream())
.mapToInt(Integer::intValue)
.toArray();
}
}
Explanation: Use Stream API for concatenation instead of manual copying.
Time Complexity: O(n) - stream operations Space Complexity: O(n) - same as main approach
Distribution Strategy:
Edge Cases:
n = 2: Return [nums[0], nums[1]] (no distribution needed)Performance Characteristics: