leetcode
leetcode 2551 ~ 2600
将数组分成最小总代价的子数组 I

将数组分成最小总代价的子数组 I

难度:

标签:

题目描述

You are given an array of integers nums of length n.

The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3.

You need to divide nums into 3 disjoint contiguous subarrays.

Return the minimum possible sum of the cost of these subarrays.

 

Example 1:

Input: nums = [1,2,3,12]
Output: 6
Explanation: The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.

Example 2:

Input: nums = [5,4,3]
Output: 12
Explanation: The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
It can be shown that 12 is the minimum cost achievable.

Example 3:

Input: nums = [10,3,1,1]
Output: 12
Explanation: The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
It can be shown that 12 is the minimum cost achievable.

 

Constraints:

  • 3 <= n <= 50
  • 1 <= nums[i] <= 50

代码结果

运行时间: 21 ms, 内存: 16.1 MB


/*
 * Problem: You are given an integer array nums of length n.
 * The cost of an array is its first element. For example, the cost of [1,2,3] is 1.
 * You need to divide nums into 3 contiguous and non-overlapping subarrays.
 * Return the minimum sum of the costs of these subarrays.
 *
 * Example 1:
 * Input: nums = [1,2,3,12]
 * Output: 6
 * Explanation: The best way to split into 3 subarrays is: [1], [2], and [3,12].
 * The total cost is 1 + 2 + 3 = 6.
 * Other ways are:
 * - [1], [2,3], and [12] with a total cost of 1 + 2 + 12 = 15.
 * - [1,2], [3], and [12] with a total cost of 1 + 3 + 12 = 16.
 *
 * Example 2:
 * Input: nums = [5,4,3]
 * Output: 12
 * Explanation: The best way to split into 3 subarrays is: [5], [4], and [3].
 * The total cost is 5 + 4 + 3 = 12.
 *
 * Example 3:
 * Input: nums = [10,3,1,1]
 * Output: 12
 * Explanation: The best way to split into 3 subarrays is: [10,3], [1], and [1].
 * The total cost is 10 + 1 + 1 = 12.
 */

import java.util.Arrays;

public class Solution {
    public int minCost(int[] nums) {
        int n = nums.length;
        return Arrays.stream(nums).sorted().limit(3).sum();
    }
}

解释

方法:

这个题解的思路是首先保留数组的第一个元素,然后将数组的剩余部分进行排序。排序后,为了最小化总代价,选择排序后的前三个元素(包括第一个原始元素)作为三个子数组的首元素。这样做是基于假设,排序后的前三个元素将会是最小的三个数,从而保证分割成三个子数组后的总代价最低。

时间复杂度:

O(n log n)

空间复杂度:

O(n)

代码细节讲解

🦆
题解中提到将数组的剩余部分进行排序,这样做是否会影响原数组的连续性,从而使得分割出的子数组不再是连续的?
是的,题解中的方法确实会影响原数组的连续性。排序操作改变了元素的原始顺序,导致无法从排序后的数组中直接得到原数组的连续子数组。因此,这种方法不能保证分割出的子数组满足题目要求的连续性。
🦆
题解中选择排序后数组的前三个元素作为子数组的首元素,这种方法是否确保了这三个元素在原数组中的位置能够划分出三个连续的子数组?
这种方法不能确保能在原数组中找到三个连续的子数组,使得每个子数组的首元素是排序后的前三个元素。排序操作改变了元素间的相对位置,所以即使这三个元素是最小的,也不意味着在原数组中它们是连续的或能形成连续的子数组。
🦆
题解假设通过选择排序后的最小三个元素作为子数组的首元素来最小化总代价,但是否考虑了分割后子数组可能包含更大的元素从而增加总代价的情况?
题解没有考虑这一点。虽然选择最小的三个元素作为各子数组的首元素可以初步降低代价,但由于子数组是连续的,后续元素可能会很大,从而实际增加了子数组的代价。因此,简单地选取最小的三个元素而不考虑它们在原数组中的位置和连续性,可能不会得到最优的总代价。

相关问题