Problem Description
Given an array of integers, the task is to divide the array into 3 disjoint contiguous subarrays. The cost of a subarray is defined as its first element. The goal is to determine the minimum possible sum of these costs.
Key Insights
- The array must be split into exactly 3 contiguous parts, meaning you need to select 2 split points.
- The cost for a subarray is only determined by its first element.
- The first element of the entire array automatically contributes to the cost.
- By choosing two indices (split positions), the cost becomes: nums[0] + nums[i] + nums[j], where i and j are the starting indices of the second and third subarrays respectively.
- Given the small constraints (n <= 50), iterating over all possible split points using a nested loop is efficient.
Space and Time Complexity
Time Complexity: O(n^2)
Space Complexity: O(1)
Solution
We can solve the problem by iterating through all possible ways to choose two breakpoints in the array. For each valid pair of breakpoints (i, j) where 0 < i < j < n, compute the total cost as nums[0] + nums[i] + nums[j]. Track the minimum cost found over all iterations.