Minimum Partition Score II Problem
Description
LeetCode Problem 3929.
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 sum = 5 and value = 5 * 6 / 2 = 15.
- The second subarray has sum = 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 sum = 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 sum = 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 <= 5 * 10^41 <= nums[i] <= 10^31 <= k <= nums.length
Solution
Approach: Lagrangian Relaxation (Aliens Trick) + Convex Hull Trick
Let F(c) be the best score with exactly c parts. Every part costs T(s) = s(s + 1) / 2 for its sum s, which is convex, so F is convex in c. That lets us drop the "exactly k parts" condition: charge an extra lam for each part and let f(lam) = min over c of F(c) + lam * c. Then F(k) = max over lam of f(lam) - lam * k, and the function being maximised is concave in lam.
To evaluate f(lam) we solve a plain partition DP with prefix sums s:
dp[i] = min over j < i of dp[j] + T(s[i] - s[j]) + lam
Expanding T shows that each j is a line with slope -s[j] in the variable s[i]. Slopes decrease and queries increase, so a monotone deque (convex hull trick) gives O(n) per evaluation. Intersections are compared with floor division, so everything stays in 64-bit integers.
We also track how many parts the best partition uses. If it uses more than k, the best lam is larger; if fewer, it is smaller. This binary search narrows lam to a couple of values; evaluate f(lam) - lam * k for them and take the maximum. The largest useful lam is about F(1) / (k - 1), which bounds the search.
Complexity: O(n log V) time, where V is the total sum squared (about 50 rounds of the O(n) DP), and O(n) extra space.
class Solution:
def minPartitionScore(self, nums: List[int], k: int) -> int:
n = len(nums)
s = [0] * (n + 1)
for i, x in enumerate(nums):
s[i + 1] = s[i] + x
one = (s[n] * s[n] + s[n]) // 2
if k == 1:
return one
# Penalised version: every part costs an extra `lam`. Returns (best score, number of parts).
# dp[i] = min over j < i of dp[j] + (s_i^2 + s_i) / 2 + (s_j^2 - s_j) / 2 - s_i * s_j + lam,
# a minimum over lines with slope -s_j, queried at the increasing points x = s_i.
def solve(lam: int):
ms, bs, cs = [0], [0], [0]
head = 0
val = cnt = 0
for i in range(1, n + 1):
x = s[i]
while len(ms) - head >= 2 and ms[head] * x + bs[head] >= ms[head + 1] * x + bs[head + 1]:
head += 1
val = ms[head] * x + bs[head] + (x * x + x) // 2 + lam
cnt = cs[head] + 1
m, b = -x, val + (x * x - x) // 2
while len(ms) - head >= 2:
f12 = (bs[-1] - bs[-2]) // (ms[-2] - ms[-1])
f23 = (b - bs[-1]) // (ms[-1] - m)
if f23 <= f12:
ms.pop()
bs.pop()
cs.pop()
else:
break
ms.append(m)
bs.append(b)
cs.append(cnt)
return val, cnt
# The best score with exactly k parts is the maximum over lam of solve(lam) - lam * k (it is concave in lam).
lo, hi = 0, one // (k - 1) + 1
while hi - lo > 2:
mid = (lo + hi) // 2
_, cnt = solve(mid)
if cnt > k:
lo = mid
elif cnt < k:
hi = mid
else:
lo = hi = mid
return max(solve(lam)[0] - lam * k for lam in range(lo, hi + 1))