leetcode
leetcode 2751 ~ 2800
将元素分配到两个数组中 I

将元素分配到两个数组中 I

难度:

标签:

题目描述

You are given a 1-indexed array of distinct integers nums of length n.

You need to distribute all the elements of nums between two arrays arr1 and arr2 using n operations. In the first operation, append nums[1] to arr1. In the second operation, append nums[2] to arr2. Afterwards, in the ith operation:

  • If the last element of arr1 is greater than the last element of arr2, append nums[i] to arr1. Otherwise, append nums[i] to arr2.

The array result is formed by concatenating the arrays arr1 and arr2. For example, if arr1 == [1,2,3] and arr2 == [4,5,6], then result = [1,2,3,4,5,6].

Return the array result.

 

Example 1:

Input: nums = [2,1,3]
Output: [2,3,1]
Explanation: After the first 2 operations, arr1 = [2] and arr2 = [1].
In the 3rd 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].

Example 2:

Input: nums = [5,4,3,8]
Output: [5,3,4,8]
Explanation: After the first 2 operations, arr1 = [5] and arr2 = [4].
In the 3rd 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 4th 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].

 

Constraints:

  • 3 <= n <= 50
  • 1 <= nums[i] <= 100
  • All elements in nums are distinct.

代码结果

运行时间: 16 ms, 内存: 16.0 MB


/*
 * 思路:
 * 1. 使用两个 LinkedList 分别表示 arr1 和 arr2。
 * 2. 将 nums[0] 添加到 arr1,将 nums[1] 添加到 arr2。
 * 3. 使用 IntStream 遍历 nums 从索引 2 开始的元素,根据条件将元素添加到 arr1 或 arr2。
 * 4. 最后使用 Stream.concat 连接 arr1 和 arr2 并转换为数组返回。
 */
import java.util.*;
import java.util.stream.*;

public class Solution {
    public int[] rearrangeArray(int[] nums) {
        LinkedList<Integer> arr1 = new LinkedList<>();
        LinkedList<Integer> arr2 = new LinkedList<>();
        arr1.add(nums[0]);
        arr2.add(nums[1]);
        IntStream.range(2, nums.length).forEach(i -> {
            if (arr1.getLast() > arr2.getLast()) {
                arr1.add(nums[i]);
            } else {
                arr2.add(nums[i]);
            }
        });
        return Stream.concat(arr1.stream(), arr2.stream()).mapToInt(i -> i).toArray();
    }
}

解释

方法:

题解首先初始化两个数组arr1和arr2,将nums数组的前两个元素分别放入这两个数组。然后,从第三个元素开始,依次判断arr1的最后一个元素和arr2的最后一个元素的大小。如果arr1的最后一个元素大于arr2的最后一个元素,则将当前元素添加到arr1中;否则,添加到arr2中。遍历完成后,将arr2的所有元素追加到arr1后面,返回arr1作为结果。这种方法直接根据题目要求进行元素的分配,然后合并两个数组形成最终结果。

时间复杂度:

O(n)

空间复杂度:

O(n)

代码细节讲解

🦆
在处理只有两个元素的数组时,题解中提到直接将第一个元素放入arr1,第二个元素放入arr2,这样的分配是否总是满足题目要求?
题解的方法在处理只有两个元素的数组时直接将第一个元素放入arr1,第二个元素放入arr2,这种做法简单且符合题目的基本要求,即将数组分配到两个子数组中。在这种情况下,由于题目没有提供特别的排序或其他条件,这种分配方式是合理的,能够满足题目的基本操作规则。
🦆
题解中提到如果数组只有一个元素,直接返回数组,这种情况是否符合题目的操作规则,即是否应该将该单一元素放入arr1?
题解中提到的处理单一元素数组的方法是直接返回该数组。这种处理方式符合题目的基本操作规则,因为题目要求是将元素分配到两个数组中,如果只有一个元素,那么可以认为它已经被分配到一个数组中(arr1),而arr2为空。因此,直接返回该元素作为arr1的结果是合理的。
🦆
在将arr2的所有元素追加到arr1后,是否需要考虑arr1和arr2的元素总数是否等于nums的长度,以保证没有元素被遗漏?
题解中将arr2的所有元素追加到arr1后,确实应该验证arr1的元素总数是否等于nums的长度,以确保没有元素被遗漏。这是一个重要的验证步骤,可以通过比较arr1的长度和nums的长度来进行。如果长度相等,则说明所有元素都被正确处理和包含在最终结果中;如果不等,则表明在分配或合并的过程中可能有遗漏或错误。

相关问题