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 <= 2001 <= nums[i] <= 1060 <= k <= nums.length - 1Problem Overview: You receive an array nums where each value represents the number of elements stored at a time step. The array capacity can be resized at most k times. Each segment must allocate capacity equal to the maximum value in that segment. The goal is to minimize the total unused space across the entire sequence.
Approach 1: Greedy Capacity Expansion (Approximate) (Time: O(n^2), Space: O(1))
This idea grows segments greedily while tracking the current maximum element. For every starting index, extend the segment forward and compute wasted space as max_value * length - sum. The greedy intuition is that increasing capacity only when necessary reduces resizing operations. However, this strategy does not always guarantee the global minimum waste because early greedy choices may force inefficient capacity later. It works as a baseline and helps visualize how segment capacity determines waste.
Approach 2: Dynamic Programming with Partitioning (Optimal) (Time: O(n^2 * k), Space: O(n * k))
This approach models the problem as partitioning the array into at most k + 1 segments. Define dp[i][j] as the minimum wasted space for the first i elements using j resizing operations. For each state, iterate backward to form the last segment ending at i, while maintaining the segment's maximum value and cumulative sum. The wasted space for that segment is computed in constant time during the iteration. Transition: dp[i][j] = min(dp[p][j-1] + waste(p+1..i)). This partition-based DP ensures every possible segmentation is evaluated efficiently.
The key observation: once a segment's capacity is fixed to its maximum element, every other value contributes predictable unused space. By iterating backward and updating the running maximum, the cost of each candidate segment can be computed without extra preprocessing.
This technique is a classic example of segmentation DP where the array is split into optimal groups. Problems involving partitioning arrays with minimized cost frequently use dynamic programming over prefixes combined with scanning segments. The input itself is a sequential structure, so operations revolve around scanning the array and maintaining segment statistics.
Recommended for interviews: Dynamic Programming with partitioning. Interviewers expect you to recognize the segmentation pattern and construct a dp[i][k] state. Starting with the greedy intuition helps explain the cost calculation, but the DP solution demonstrates strong problem decomposition and optimization skills.
This approach involves using dynamic programming to solve the problem of minimizing space wasted by partitioning the array into subarrays. Each subarray has a uniform size that is at least the maximum of the subarray's elements. Work through the subarrays and decide the partition points based on available resizing operations (k) and minimize the total space wasted.
This Python solution initializes a 2D dp array where dp[i][j] represents the minimum space wasted for the first i elements with j resizings. The solution iteratively computes the space wasted for different segmentations, updating dp values by considering the maximum size needed for each segment. The innermost loop works backward from the current position, testing different starting points for the current portion size-wise.
Time Complexity: O(n^2 * k) where n is the number of elements in nums. Space Complexity: O(n * k).
This approach uses a greedy strategy, striving to always take the locally optimal configuration regarding resizing at each step. By making locally optimal choices, one can achieve the minimum possible segment space waste.
In this Java solution, a greedy technique reiterates across input numbers to compute space waste by proactively reallocating constructs to hold segment-wise maximal figures, anchoring at the least total space waste. The most competitive and efficient configuration is derived by sustaining a thorough lookup of resourceful restructuring decisions reflected throughout iterations.
Java
JavaScript
Time Complexity: O(n^2 * k), Space Complexity: O(n * k).
The problem is equivalent to dividing the array nums into k + 1 segments. The wasted space for each segment is the maximum value of that segment multiplied by the length of the segment minus the sum of the elements in that segment. By summing the wasted space of each segment, we get the total wasted space. By adding 1 to k, we are effectively dividing the array into k segments.
Therefore, we define an array g[i][j] to represent the wasted space for the segment nums[i..j], which is the maximum value of nums[i..j] multiplied by the length of nums[i..j] minus the sum of the elements in nums[i..j]. We iterate over i in the range [0, n) and j in the range [i, n), using a variable s to maintain the sum of the elements in nums[i..j] and a variable mx to maintain the maximum value of nums[i..j]. Then we can get:
$ g[i][j] = mx times (j - i + 1) - s
Next, we define f[i][j] to represent the minimum wasted space for dividing the first i elements into j segments. We initialize f[0][0] = 0 and the other positions to infinity. We iterate over i in the range [1, n] and j in the range [1, k], then we iterate over the last element h of the previous j - 1 segments. Then we have:
f[i][j] = min(f[i][j], f[h][j - 1] + g[h][i - 1])
The final answer is f[n][k].
The time complexity is O(n^2 times k), and the space complexity is O(n times (n + k)). Where n is the length of the array nums$.
| Approach | Complexity |
|---|---|
| Dynamic Programming with Partitioning | Time Complexity: O(n^2 * k) where n is the number of elements in nums. Space Complexity: O(n * k). |
| Greedy Method | Time Complexity: O(n^2 * k), Space Complexity: O(n * k). |
| Dynamic Programming | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Capacity Expansion | O(n^2) | O(1) | Quick heuristic or conceptual baseline for understanding capacity waste |
| Dynamic Programming with Partitioning | O(n^2 * k) | O(n * k) | General optimal solution when up to k resizes are allowed |
Leetcode 1959. Minimum Total Space Wasted With K Resizing Operations | C++ | in HINDI • Nitesh Kumar • 1,796 views views
Watch 3 more video solutions →Practice Minimum Total Space Wasted With K Resizing Operations with our built-in code editor and test cases.
Practice on FleetCode