You are given an integer array nums and an integer k.
Your task is to partition nums into exactly k subarrays and return an integer denoting the minimum possible score among all valid partitions.
The score of a partition is the sum of the values of all its subarrays.
The value of a subarray is defined as sumArr * (sumArr + 1) / 2, where sumArr is the sum of its elements.
Example 1:
Input: nums = [5,1,2,1], k = 2
Output: 25
Explanation:
k = 2 subarrays. One optimal partition is [5] and [1, 2, 1].sum = 5 and value = 5 * 6 / 2 = 15.sum = 1 + 2 + 1 = 4 and value = 4 * 5 / 2 = 10.15 + 10 = 25, which is the minimum possible score.Example 2:
Input: nums = [1,2,3,4], k = 1
Output: 55
Explanation:
k = 1 subarray, all elements belong to the same subarray: [1, 2, 3, 4].sum = 1 + 2 + 3 + 4 = 10 and value = 10 * 11 / 2 = 55.Example 3:
Input: nums = [1,1,1], k = 3
Output: 3
Explanation:
k = 3 subarrays. The only valid partition is [1], [1], [1].sum = 1 and value = 1 * 2 / 2 = 1.1 + 1 + 1 = 3, which is the minimum possible score.
Constraints:
1 <= nums.length <= 5 * 1041 <= nums[i] <= 1031 <= k <= nums.lengthProblem Overview: You are given an array and must split it into multiple contiguous partitions so that the total partition score is minimized. The score of each segment depends on values inside that subarray, so the challenge is choosing optimal cut points.
Approach 1: Brute Force Partition Enumeration (O(n^3) time, O(n) space)
The most direct method tries every possible partition configuration. For each index i, treat it as the end of the current segment and iterate over all possible starting points j. Compute the segment score for nums[j..i] and combine it with the best score before j. If computing segment scores requires scanning the subarray, each check costs O(n), leading to O(n^3) total complexity. This approach is useful for understanding the recurrence but becomes impractical for large inputs.
Approach 2: Dynamic Programming with Prefix Precomputation (O(n^2) time, O(n) space)
Define dp[i] as the minimum partition score for the prefix ending at index i. For each i, iterate backward through possible previous partition points j. The transition becomes dp[i] = min(dp[j] + score(j+1, i)). Precompute segment statistics using prefix sums or other auxiliary arrays so the score of any subarray can be evaluated in constant time. This reduces the runtime to O(n^2). Most accepted solutions rely on this DP structure because it is straightforward and handles general scoring functions.
Approach 3: Optimized DP with Segment Tree / Monotonic Structure (O(n log n) time, O(n) space)
When the partition score has monotonic or decomposable properties, the transition can be optimized. Maintain candidate transitions using a segment tree or monotonic structure that stores the best previous DP states. Instead of scanning all j for every i, query the data structure for the optimal value over a valid range. Each update and query costs O(log n), reducing the total complexity to O(n log n). This pattern commonly appears in advanced dynamic programming problems where transitions depend on range queries.
Recommended for interviews: The O(n^2) dynamic programming approach is usually expected. It clearly demonstrates the recurrence and correct state definition. Starting with the brute-force idea shows understanding of the partition structure, while moving to optimized DP demonstrates algorithmic maturity. If constraints are large, discussing a segment tree or monotonic optimization signals strong problem-solving depth.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Partition Enumeration | O(n^3) | O(n) | Understanding the partition recurrence or when constraints are very small |
| Dynamic Programming with Prefix Precomputation | O(n^2) | O(n) | General solution that works for most scoring definitions |
| DP with Segment Tree / Range Optimization | O(n log n) | O(n) | Large constraints where DP transitions require efficient range minimum queries |
Practice Minimum Partition Score II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor