Divide an Array Into Subarrays With Minimum Cost I

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.

Example 1
Inputnums = [1,2,3,12]
Output6
The best possible way to form 3 subarrays is [1], [2], and [3, 12], with total cost 1 + 2 + 3 = 6.
Example 2
Inputnums = [5,4,3]
Output12
The best possible way to form 3 subarrays is [5], [4], and [3], with total cost 5 + 4 + 3 = 12.

Constraints

  • 3 <= n <= 50
  • 1 <= nums[i] <= 50

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate