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, 1. Total = 6+2+1 = 9. This is minimum among all possible divisions.
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code