Divide an Array Into Subarrays With Minimum Cost I - Problem
You are given an array of integers nums of length n.
The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3.
You need to divide nums into 3 disjoint contiguous subarrays.
Return the minimum possible sum of the cost of these subarrays.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3,4]
›
Output:
6
💡 Note:
Divide into [1], [2], [3,4]. Costs are 1, 2, 3. Total = 1+2+3 = 6. This is minimum among all possible divisions.
Example 2 — Different Values
$
Input:
nums = [6,2,7,4,1]
›
Output:
9
💡 Note:
Divide into [6], [2,7], [4,1]. Costs are 6, 2, 4. Total = 6+2+4 = 12. Alternative: [6,2], [7], [4,1] gives 6+7+4=17. Best is [6], [2], [7,4,1] = 6+2+7=15. Actually [6], [2,7,4], [1] = 6+2+1=9.
Example 3 — Minimum Size
$
Input:
nums = [20,6,2]
›
Output:
28
💡 Note:
Only one way to divide into 3 subarrays: [20], [6], [2]. Costs are 20, 6, 2. Total = 20+6+2 = 28.
Constraints
- 3 ≤ nums.length ≤ 50
- 1 ≤ nums[i] ≤ 50
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array [1,2,3,4] needs to be split into 3 parts
2
Make 2 Cuts
Try all valid positions to make exactly 2 cuts
3
Calculate Cost
Sum the first element of each resulting subarray
Key Takeaway
🎯 Key Insight: Cost equals sum of first elements, so try all valid cut positions to find minimum
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code