leetcode
leetcode 2701 ~ 2750
K 次调整数组大小浪费的最小总空间

K 次调整数组大小浪费的最小总空间

难度:

标签:

题目描述

You are currently designing a dynamic array. You are given a 0-indexed integer array nums, where nums[i] is the number of elements that will be in the array at time i. In addition, you are given an integer k, the maximum number of times you can resize the array (to any size).

The size of the array at time t, sizet, must be at least nums[t] because there needs to be enough space in the array to hold all the elements. The space wasted at time t is defined as sizet - nums[t], and the total space wasted is the sum of the space wasted across every time t where 0 <= t < nums.length.

Return the minimum total space wasted if you can resize the array at most k times.

Note: The array can have any size at the start and does not count towards the number of resizing operations.

 

Example 1:

Input: nums = [10,20], k = 0
Output: 10
Explanation: size = [20,20].
We can set the initial size to be 20.
The total wasted space is (20 - 10) + (20 - 20) = 10.

Example 2:

Input: nums = [10,20,30], k = 1
Output: 10
Explanation: size = [20,20,30].
We can set the initial size to be 20 and resize to 30 at time 2. 
The total wasted space is (20 - 10) + (20 - 20) + (30 - 30) = 10.

Example 3:

Input: nums = [10,20,15,30,20], k = 2
Output: 15
Explanation: size = [10,20,20,30,30].
We can set the initial size to 10, resize to 20 at time 1, and resize to 30 at time 3.
The total wasted space is (10 - 10) + (20 - 20) + (20 - 15) + (30 - 30) + (30 - 20) = 15.

 

Constraints:

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 106
  • 0 <= k <= nums.length - 1

代码结果

运行时间: 805 ms, 内存: 16.7 MB


/*
思路:
1. 使用Java Stream API来计算浪费空间。
2. 定义一个函数来计算某个数组截断点的浪费空间。
3. 使用Stream API的collect方法来实现动态规划。
*/

import java.util.*;
import java.util.stream.*;

public class MinWastedSpaceStream {
    public int minWastedSpace(int[] nums, int k) {
        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(0, k + 1).forEach(j -> {
            int[] maxNum = {0};
            int[] wastedSpace = {0};
            IntStream.iterate(i, l -> l > 0, l -> l - 1).forEach(l -> {
                maxNum[0] = Math.max(maxNum[0], nums[l - 1]);
                wastedSpace[0] += maxNum[0] - nums[l - 1];
                if (j > 0 && dp[l - 1][j - 1] != Integer.MAX_VALUE) {
                    dp[i][j] = Math.min(dp[i][j], dp[l - 1][j - 1] + wastedSpace[0]);
                }
            });
        }));

        return IntStream.range(0, k + 1).map(j -> dp[n][j]).min().orElse(Integer.MAX_VALUE);
    }
}

解释

方法:

此题解采用动态规划和几何优化方法。初始阶段,它构建一个最大的DP数组,每个元素表示从起始到当前位置的最小浪费空间。随后,为了处理k次调整的问题,使用数据结构(如SkipList)来优化查找和更新操作,确保每次调整都是在最优的位置。在每一轮中,它尝试确定新的调整点,并计算从该点到下一个可能的调整点之间的最小浪费空间。最终,这种方法可以在给定调整次数的限制下,找到总浪费空间最小的策略。

时间复杂度:

O(k * N^2)

空间复杂度:

O(N)

代码细节讲解

🦆
message
The provided question list contains only the word 'message' which does not specify a clear question about the algorithm or its implementation. To provide a meaningful response, please specify the question related to the logic, boundary details, or data structure characteristics of the provided solution for the LeetCode problem.

相关问题