Divide an Array Into Subarrays With Minimum Cost I - Problem

You're given an array of integers nums with n elements, and your task is to divide it into exactly 3 disjoint contiguous subarrays.

The cost of any subarray is defined as the value of its first element. For example:

  • The cost of [1, 2, 3] is 1
  • The cost of [3, 4, 1] is 3

Your goal is to find the minimum possible sum of the costs of these three subarrays. Since we need exactly 3 subarrays, we need to place 2 dividers in the array, creating 3 contiguous segments.

Key insight: The first subarray always starts at index 0, so its cost is always nums[0]. We only need to choose where the second and third subarrays begin!

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, 2, 3, 4, 5]
โ€บ Output: 6
๐Ÿ’ก Note: Divide into [1], [2], [3, 4, 5]. Costs are 1 + 2 + 3 = 6. This is optimal because we take the three smallest possible starting elements.
example_2.py โ€” Different Values
$ Input: [5, 4, 3]
โ€บ Output: 12
๐Ÿ’ก Note: Must divide into [5], [4], [3]. Since we need exactly 3 subarrays, costs are 5 + 4 + 3 = 12. No other division is possible with length 3.
example_3.py โ€” Strategic Division
$ Input: [10, 3, 1, 1]
โ€บ Output: 12
๐Ÿ’ก Note: Optimal division is [10], [3], [1, 1]. Costs are 10 + 3 + 1 = 14. Alternative [10], [3, 1], [1] gives 10 + 3 + 1 = 14. Both are the same optimal cost.

Visualization

Tap to expand
Array Division into 3 SubarraysOriginal Array10311210Subarray 1Cost = 103Subarray 2Cost = 3[1, 1, 2]Subarray 3Cost = 1Cut 1Cut 2Total Cost Calculation10 + 3 + 1 = 14Sum of first elementsKey StrategyFirst subarray cost is fixed (nums[0])Minimize sum of starting elements of subarrays 2 and 3
Understanding the Visualization
1
Understand the Problem
We must create exactly 3 contiguous subarrays, and each subarray's cost is its first element
2
Identify the Fixed Cost
The first subarray always starts at index 0, so its cost nums[0] is unchangeable
3
Find Optimal Cuts
We need to place 2 cuts to create 3 pieces. Focus on minimizing the starting elements of pieces 2 and 3
4
Apply Strategy
For each possible start of the second subarray, find the minimum possible start for the third subarray
Key Takeaway
๐ŸŽฏ Key Insight: Since the first subarray always starts at index 0, focus on finding the optimal positions for the second and third subarrays to minimize their starting element values.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

In worst case, we still need to check all valid combinations, but with optimizations

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 3 โ‰ค nums.length โ‰ค 50
  • 1 โ‰ค nums[i] โ‰ค 50
  • The array must be divided into exactly 3 non-empty contiguous subarrays
Asked in
Google 25 Amazon 20 Microsoft 15 Meta 12
28.5K Views
Medium Frequency
~15 min Avg. Time
890 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