Minimum Partition Score Problem
Description
LeetCode Problem 3826.
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:
- We must partition the array into k = 2 subarrays. One optimal partition is [5] and [1, 2, 1].
- The first subarray has sumArr = 5 and value = 5 × 6 / 2 = 15.
- The second subarray has sumArr = 1 + 2 + 1 = 4 and value = 4 × 5 / 2 = 10.
- The score of this partition is 15 + 10 = 25, which is the minimum possible score.
Example 2:
Input: nums = [1,2,3,4], k = 1
Output: 55
Explanation:
- Since we must partition the array into k = 1 subarray, all elements belong to the same subarray: [1, 2, 3, 4].
- This subarray has sumArr = 1 + 2 + 3 + 4 = 10 and value = 10 × 11 / 2 = 55.
- The score of this partition is 55, which is the minimum possible score.
Example 3:
Input: nums = [1,1,1], k = 3
Output: 3
Explanation:
- We must partition the array into k = 3 subarrays. The only valid partition is [1], [1], [1].
- Each subarray has sumArr = 1 and value = 1 × 2 / 2 = 1.
- The score of this partition is 1 + 1 + 1 = 3, which is the minimum possible score.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 10^41 <= k <= nums.length
Solution
Approach: DP with Divide and Conquer Optimization
Let P be the prefix sums of nums and let dp[t][i] be the minimum score of splitting the first i elements into exactly t subarrays. A last subarray covering elements j..i-1 has sum s = P[i] - P[j] and value s * (s + 1) / 2, so
dp[t][i] = min over j < i of dp[t-1][j] + s * (s + 1) / 2.
Trying every j costs O(n^2 k), which is too slow. The value of a subarray is convex in its sum, so the best split point opt(i) never decreases as i grows. That lets us fill one layer by divide and conquer: solve the middle index mid by scanning the allowed range of j, find its best j*, then solve the left half using j <= j* and the right half using j >= j*.
Only indices t <= i <= n - (k - t) can belong to layer t (there must be enough elements before and after), which keeps every dp[t-1][j] that we read valid. The answer is dp[k][n]. Scores can reach about 5 * 10^13, so use 64-bit integers.
Complexity: O(k n log n) time and O(n) space, where n is the length of nums.
class Solution:
def minPartitionScore(self, nums: List[int], k: int) -> int:
n = len(nums)
P = [0] * (n + 1)
for i, x in enumerate(nums):
P[i + 1] = P[i] + x
# layer 1: the whole prefix is a single subarray
prev = [p * (p + 1) // 2 for p in P]
for t in range(2, k + 1):
cur = [0] * (n + 1)
def solve(lo: int, hi: int, ol: int, oh: int) -> None:
if lo > hi:
return
mid = (lo + hi) // 2
best, bj = None, ol
for j in range(ol, min(mid - 1, oh) + 1):
s = P[mid] - P[j]
v = prev[j] + s * (s + 1) // 2
if best is None or v < best:
best, bj = v, j
cur[mid] = best
solve(lo, mid - 1, ol, bj)
solve(mid + 1, hi, bj, oh)
solve(t, n - (k - t), t - 1, n - 1)
prev = cur
return prev[n]