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

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

难度:

标签:

题目描述

You are given a 0-indexed array of integers nums of length n, and two positive integers k and dist.

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 k disjoint contiguous subarrays, such that the difference between the starting index of the second subarray and the starting index of the kth subarray should be less than or equal to dist. In other words, if you divide nums into the subarrays nums[0..(i1 - 1)], nums[i1..(i2 - 1)], ..., nums[ik-1..(n - 1)], then ik-1 - i1 <= dist.

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

 

Example 1:

Input: nums = [1,3,2,6,4,2], k = 3, dist = 3
Output: 5
Explanation: The best possible way to divide nums into 3 subarrays is: [1,3], [2,6,4], and [2]. This choice is valid because ik-1 - i1 is 5 - 2 = 3 which is equal to dist. The total cost is nums[0] + nums[2] + nums[5] which is 1 + 2 + 2 = 5.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 5.

Example 2:

Input: nums = [10,1,2,2,2,1], k = 4, dist = 3
Output: 15
Explanation: The best possible way to divide nums into 4 subarrays is: [10], [1], [2], and [2,2,1]. This choice is valid because ik-1 - i1 is 3 - 1 = 2 which is less than dist. The total cost is nums[0] + nums[1] + nums[2] + nums[3] which is 10 + 1 + 2 + 2 = 15.
The division [10], [1], [2,2,2], and [1] is not valid, because the difference between ik-1 and i1 is 5 - 1 = 4, which is greater than dist.
It can be shown that there is no possible way to divide nums into 4 subarrays at a cost lower than 15.

Example 3:

Input: nums = [10,8,18,9], k = 3, dist = 1
Output: 36
Explanation: The best possible way to divide nums into 4 subarrays is: [10], [8], and [18,9]. This choice is valid because ik-1 - i1 is 2 - 1 = 1 which is equal to dist.The total cost is nums[0] + nums[1] + nums[2] which is 10 + 8 + 18 = 36.
The division [10], [8,18], and [9] is not valid, because the difference between ik-1 and i1 is 3 - 1 = 2, which is greater than dist.
It can be shown that there is no possible way to divide nums into 3 subarrays at a cost lower than 36.

 

Constraints:

  • 3 <= n <= 105
  • 1 <= nums[i] <= 109
  • 3 <= k <= n
  • k - 2 <= dist <= n - 2

代码结果

运行时间: 370 ms, 内存: 30.8 MB


/*
 * 思路:
 * 1. 使用 Java Stream API 重构动态规划方法。
 * 2. dp[i][j] 表示将前 i 个元素分成 j 段的最小总代价。
 * 3. 我们需要考虑最后一段的起点,并保证其起点与第一段的起点距离不超过 dist。
 */

import java.util.Arrays;
import java.util.stream.IntStream;

public class Solution {
    public int minCost(int[] nums, int k, int dist) {
        int n = nums.length;
        int[][] dp = new int[n + 1][k + 1];
        for (int i = 0; i <= n; i++) {
            Arrays.fill(dp[i], Integer.MAX_VALUE);
        }
        dp[0][0] = 0;
        IntStream.range(1, n + 1).forEach(i -> {
            IntStream.range(1, k + 1).forEach(j -> {
                IntStream.range(0, i).filter(m -> i - m <= dist + 1)
                    .forEach(m -> dp[i][j] = Math.min(dp[i][j], dp[m][j - 1] + nums[m]));
            });
        });
        return IntStream.range(k, n + 1).map(i -> dp[i][k]).min().orElse(Integer.MAX_VALUE);
    }
}

解释

方法:

该题解使用了两个堆(最大堆和最小堆)以及一个哈希表来维护和更新窗口中的元素,其中窗口大小由 dist 控制。窗口代表了可以作为子数组起始点的范围。解题思路的核心在于动态地维护k个子数组的最小总代价,通过在窗口中添加新元素,并从窗口移除旧元素。最大堆(pickedHeap)用于维护已选择的元素(负值表示,因为Python的heapq实现的是最小堆),而最小堆(unpickedHeap)用于维护未选择的元素。当一个新元素大于最大堆的堆顶时,它被加入到最小堆;反之,则替换最大堆的堆顶元素,并将旧的堆顶元素移到最小堆。这样可以保证当前窗口内的k个元素中,最小的元素总是位于最大堆的堆顶。哈希表(removesBook)用于记录已经从窗口中移除的元素的计数,以便在更新堆时正确处理这些元素。

时间复杂度:

O(n log k)

空间复杂度:

O(n)

代码细节讲解

🦆
在算法中使用两个堆(最大堆和最小堆)的原因是什么?
在算法中使用两个堆可以有效地维护当前窗口内元素的最小总代价。最大堆(使用负值实现)用于追踪窗口中的最小元素,因为这些元素直接影响总代价。最小堆则用来存储未被选为最小元素的其他元素。通过这种方式,每当窗口移动或元素更新时,可以快速地从最大堆中获取最小元素,同时将新的较小元素加入最大堆,并将原最小元素移至最小堆,从而保证在动态变化的窗口中总是能够迅速地计算和更新最小总代价。
🦆
为什么需要使用哈希表来记录已从窗口中移除的元素的计数?具体是如何配合堆的操作来使用这些记录的?
使用哈希表记录已从窗口中移除的元素的计数是为了正确管理和更新堆中的元素。由于堆结构本身不支持随机访问,当窗口滑动导致某些元素不再属于当前窗口时,这些元素可能仍在堆中。通过哈希表记录这些元素的计数,我们可以在执行堆操作(如弹出元素)时检查顶部元素是否已被移出窗口,如果是,则将其弹出并调整堆,直到堆顶是有效的窗口元素。这样确保了堆中的数据始终反映当前窗口的状态,从而正确计算最小总代价。
🦆
算法中的堆操作(插入和移除)如何确保始终保持k个子数组的最小总代价?
堆操作在算法中确保了每次窗口更新后,最大堆始终包含当前窗口中的最小元素,从而保持了k个子数组的最小总代价。具体来说,每当新元素加入窗口时,会与最大堆的堆顶(当前最小元素)比较,根据比较结果决定是替换堆顶还是加入最小堆。当窗口移动导致元素退出时,通过哈希表和堆操作来确保移除的是正确的元素,并从最小堆中补充元素到最大堆,保持最大堆的大小和堆顶元素的正确性。通过这些操作,算法可以持续调整并保持最小总代价。

相关问题