Watch 2 video solutions for Minimum Cost to Divide Array Into Subarrays, a hard level problem involving Array, Dynamic Programming, Prefix Sum. This walkthrough by Programming Live with Larry has 671 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given two integer arrays, nums and cost, of the same size, and an integer k.
You can divide nums into subarrays. The cost of the ith subarray consisting of elements nums[l..r] is:
(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r]).Note that i represents the order of the subarray: 1 for the first subarray, 2 for the second, and so on.
Return the minimum total cost possible from any valid division.
Example 1:
Input: nums = [3,1,4], cost = [4,6,6], k = 1
Output: 110
Explanation:
The minimum total cost possible can be achieved by dividingnums into subarrays [3, 1] and [4].
[3,1] is (3 + 1 + 1 * 1) * (4 + 6) = 50.[4] is (3 + 1 + 4 + 1 * 2) * 6 = 60.Example 2:
Input: nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7
Output: 985
Explanation:
The minimum total cost possible can be achieved by dividingnums into subarrays [4, 8, 5, 1], [14, 2, 2], and [12, 1].
[4, 8, 5, 1] is (4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525.[14, 2, 2] is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250.[12, 1] is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210.
Constraints:
1 <= nums.length <= 1000cost.length == nums.length1 <= nums[i], cost[i] <= 10001 <= k <= 1000Problem Overview: You are given an array and must split it into multiple contiguous subarrays so the total cost of all chosen segments is minimized. The challenge is deciding where to place partition boundaries while computing subarray costs efficiently.
Approach 1: Brute Force Partition Enumeration (O(n^3) time, O(1) space)
The naive strategy tries every possible way to split the array. For each starting index i, iterate over all possible ending indices j to form a subarray, compute its cost directly by scanning the elements, and recursively evaluate the remaining suffix. Because computing each subarray cost may take O(n) and there are O(n^2) candidate segments, the total runtime grows to O(n^3). This approach is mainly useful for understanding the structure of the problem before introducing optimization.
Approach 2: Dynamic Programming with Prefix Sum (O(n^2) time, O(n) space)
The standard solution uses dynamic programming. Define dp[i] as the minimum cost to divide the prefix ending at index i. For every position i, iterate backward over possible previous cut points j. The transition becomes dp[i] = min(dp[j] + cost(j+1..i)). To avoid recomputing segment costs repeatedly, precompute cumulative sums using prefix sums. This allows constant‑time cost calculation for any subarray. The DP processes n states and checks up to n previous cuts, giving O(n^2) time and O(n) memory.
Approach 3: DP with Precomputation of Segment Costs (O(n^2) time, O(n^2) space)
Another practical variant precomputes the cost of every subarray cost[i][j] using prefix sums or incremental updates. After building this table, the DP transition becomes a direct lookup when evaluating partitions. This removes repeated arithmetic inside the DP loop and can simplify implementation, though it increases memory usage to O(n^2). It still performs O(n^2) transitions but often runs faster in practice because each state update becomes a constant‑time table lookup.
Recommended for interviews: Interviewers expect the dynamic programming approach with prefix sums. Start by describing the brute force partition idea to show understanding of the search space, then transition to the optimized DP formulation. Using array traversal with prefix sums to compute segment costs in constant time demonstrates strong problem‑solving and optimization skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Partition Enumeration | O(n^3) | O(1) | Conceptual baseline or very small arrays |
| Dynamic Programming with Prefix Sum | O(n^2) | O(n) | General optimal solution for most constraints |
| DP with Precomputed Segment Costs | O(n^2) | O(n^2) | When memory is available and faster constant factors are desired |