Skip to main content

Minimum Cost to Divide Array Into Subarrays - Solution & Explanation

Practice this problem

Problem Statement

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 dividing nums into subarrays [3, 1] and [4].
  • The cost of the first subarray [3,1] is (3 + 1 + 1 * 1) * (4 + 6) = 50.
  • The cost of the second subarray [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 dividing nums into subarrays [4, 8, 5, 1], [14, 2, 2], and [12, 1].
  • The cost of the first subarray [4, 8, 5, 1] is (4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525.
  • The cost of the second subarray [14, 2, 2] is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250.
  • The cost of the third subarray [12, 1] is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210.

 

Constraints:

  • 1 <= nums.length <= 1000
  • cost.length == nums.length
  • 1 <= nums[i], cost[i] <= 1000
  • 1 <= k <= 1000

Approach Overview

Problem 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.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Partition EnumerationO(n^3)O(1)Conceptual baseline or very small arrays
Dynamic Programming with Prefix SumO(n^2)O(n)General optimal solution for most constraints
DP with Precomputed Segment CostsO(n^2)O(n^2)When memory is available and faster constant factors are desired

Video Solution

3500. Minimum Cost to Divide Array Into Subarrays (Leetcode Hard) • Programming Live with Larry • 684 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Minimum Cost to Divide Array Into Subarrays easy or hard?
Minimum Cost to Divide Array Into Subarrays is categorized as a Hard problem because it requires recognizing a partition DP pattern and combining it with prefix sums for efficient subarray cost computation. Handling transitions and optimizing repeated cost calculations makes it more complex than standard DP problems.
Minimum Cost to Divide Array Into Subarrays Python/Java solution
Implement a DP array where dp[i] represents the minimum cost for the first i elements. Iterate over possible previous cut positions j and update dp[i] using dp[j] plus the cost of subarray (j+1..i) computed from prefix sums. This approach translates cleanly to Python, Java, C++, and Go with O(n^2) time complexity.
How to solve Minimum Cost to Divide Array Into Subarrays in O(n)?
A strict O(n) solution generally isn't feasible for the generic version because each index may depend on many previous partition choices. Most implementations rely on O(n^2) dynamic programming with prefix sums to evaluate subarray costs efficiently. Specialized constraints or cost formulas may allow further optimization.
What is the best approach for Minimum Cost to Divide Array Into Subarrays?
The most effective approach uses dynamic programming combined with prefix sums. Define dp[i] as the minimum cost to partition the array up to index i, then try every possible previous cut position. Prefix sums allow constant-time subarray cost computation, giving an overall time complexity of O(n^2) and space complexity of O(n).
Is Minimum Cost to Divide Array Into Subarrays asked at Google/Amazon/Meta?
Dynamic programming partition problems frequently appear in interviews at companies like Google, Amazon, and Meta. Variants that require splitting arrays or strings into optimal segments are common because they test DP state design, prefix sums, and optimization reasoning.
What data structure is used in Minimum Cost to Divide Array Into Subarrays?
The core solution relies on arrays for dynamic programming and prefix sums. The prefix sum array allows constant-time computation of subarray metrics, while the DP array stores the minimum cost for each prefix of the input array.
What is the time complexity of Minimum Cost to Divide Array Into Subarrays?
The optimal dynamic programming solution runs in O(n^2) time because each index considers up to n previous partition points. Space complexity is typically O(n) for the DP array, or O(n^2) if all subarray costs are precomputed in a table.

Ready to solve this problem?

Practice Minimum Cost to Divide Array Into Subarrays with our built-in code editor and test cases.

Practice on FleetCode