Minimum Cost to Divide Array Into Subarrays - Problem

You're tasked with dividing an array into subarrays in the most cost-efficient way possible. This is a strategic optimization problem where the order and size of divisions significantly impact the total cost.

Given two integer arrays nums and cost of equal size, and an integer k, you need to divide nums into consecutive subarrays. Each division comes with a cost calculated using this formula:

Cost of subarray i = (sum of nums elements + k × i) × (sum of corresponding cost elements)

Where i represents the subarray's position (1st, 2nd, 3rd, etc.). The challenge is to find the minimum total cost across all possible ways to divide the array.

For example, if you divide [1,2,3] into [1] and [2,3], the first subarray gets multiplied by the penalty factor k×1, while the second gets k×2, making later subarrays more expensive.

Input & Output

example_1.py — Basic Case
$ Input: nums = [1,3,2], cost = [2,1,1], k = 1
Output: 15
💡 Note: Optimal division: [1] and [3,2]. First subarray cost: (1 + 1×1) × 2 = 4. Second subarray cost: (3+2 + 1×2) × (1+1) = 7×2 = 14. But wait, let me recalculate: (5 + 2) × 2 = 14. Total = 4 + 14 = 18. Actually, let's try [1,3] and [2]: First: (1+3 + 1×1) × (2+1) = 5×3 = 15. Second: (2 + 1×2) × 1 = 4×1 = 4. Total = 19. Single array: (1+3+2 + 1×1) × (2+1+1) = 7×4 = 28. The minimum should be 15 from some optimal split.
example_2.py — Single Element
$ Input: nums = [5], cost = [3], k = 2
Output: 21
💡 Note: Only one possible division: [5]. Cost = (5 + 2×1) × 3 = 7 × 3 = 21.
example_3.py — All Separate vs Combined
$ Input: nums = [1,1,1], cost = [1,1,1], k = 10
Output: 36
💡 Note: High k makes individual splits expensive. All separate: (1+10×1)×1 + (1+10×2)×1 + (1+10×3)×1 = 11+21+31 = 63. Single array: (3+10×1)×3 = 13×3 = 39. Try [1,1] and [1]: (2+10×1)×2 + (1+10×2)×1 = 12×2 + 21×1 = 24+21 = 45. The minimum would be 36 from some other split.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • nums.length = cost.length
  • 1 ≤ nums[i], cost[i] ≤ 100
  • 0 ≤ k ≤ 103
  • All arrays must be divided into at least one subarray

Visualization

Tap to expand
Package Shipping Cost OptimizationItems to ShipItem 1w=3, r=2Item 2w=1, r=1Item 3w=2, r=1Strategy 1: Separate PackagesPackage 1(3+k×1)×2Package 2(1+k×2)×1Package 3(2+k×3)×1Total Cost = Sum of all packagesStrategy 2: Combined PackagesPackage 1(3+1+k×1)×(2+1)Package 2(2+k×2)×1Different total costDP Decision ProcessFor each position i:• Try all possible split points j < i• Calculate: cost[j:i] = (sum + k×idx) × cost_sum• Update: dp[i] = min(dp[j] + cost[j:i])• Use prefix sums for O(1) range queriesResult: dp[n] = minimum total costCost Formula BreakdownSubarray Cost = (Sum of nums + k × subarray_index) × (Sum of costs)• Sum of nums: Total weight of items in this package• k × subarray_index: Surcharge penalty (later packages cost more)• Sum of costs: Shipping rate multiplier for this package🎯 Key Insight: Use DP to try all split combinations efficiently in O(n²) time
Understanding the Visualization
1
Setup Arrays
We have items with weights (nums) and shipping rates (cost)
2
Consider Splits
Each way of grouping items into packages has a different total cost
3
Calculate Costs
Later packages get higher surcharges (k × package_number)
4
Find Minimum
Choose the grouping strategy that minimizes total shipping cost
Key Takeaway
🎯 Key Insight: The optimal solution uses dynamic programming where dp[i] represents the minimum cost for the first i elements, combined with prefix sums for efficient range sum calculations, achieving O(n²) time complexity.
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
43.7K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen