Maximize Cyclic Partition Score Problem
Description
LeetCode Problem 3743.
You are given a cyclic array nums and an integer k.
Partition nums into at most k subarrays. As nums is cyclic, these subarrays may wrap around from the end of the array back to the beginning.
The range of a subarray is the difference between its maximum and minimum values. The score of a partition is the sum of subarray ranges.
Return the maximum possible score among all cyclic partitions.
Example 1:
Input: nums = [1,2,3,3], k = 2
Output: 3
Explanation:
- Partition nums into [2, 3] and [3, 1] (wrapped around).
- The range of [2, 3] is max(2, 3) - min(2, 3) = 3 - 2 = 1.
- The range of [3, 1] is max(3, 1) - min(3, 1) = 3 - 1 = 2.
- The score is 1 + 2 = 3.
Example 2:
Input: nums = [1,2,3,3], k = 1
Output: 2
Explanation:
- Partition nums into [1, 2, 3, 3].
- The range of [1, 2, 3, 3] is max(1, 2, 3, 3) - min(1, 2, 3, 3) = 3 - 1 = 2.
- The score is 2.
Example 3:
Input: nums = [1,2,3,3], k = 4
Output: 3
Explanation:
Identical to Example 1, we partition nums into [2, 3] and [3, 1]. Note that nums may be partitioned into fewer than k subarrays.
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 10^91 <= k <= nums.length
Solution
Approach: Disjoint pairs, solved as a stock-style DP
The range of a piece is max - min, which equals the best value of nums[p] - nums[q] over two positions of the piece, in either order. So the score is the largest total of |nums[p] - nums[q]| over at most k pairs whose spans do not overlap: any gap between spans can be absorbed by a neighbouring piece, and that never lowers a range.
On a straight array this is a DP like "best time to buy and sell stock IV". For each count j of pairs we keep three values: idle (all j pairs finished), plus (the j-th pair is open and holds +x) and minus (it is open and holds -x). At each element x a pair closes with idle = max(idle, plus - x, minus + x), or opens from idle[j-1] with +x or -x.
To handle the cycle, rotate so the array starts at a maximum. Only one piece can wrap around the cut, and it contains that maximum, so it can be replaced by a piece that starts at the maximum and runs in one of the two directions. Therefore the answer is the larger of the DP on the rotated array and the DP on its reverse (keeping the first element in place).
Complexity: time O(n * min(n, k)), space O(n).
class Solution:
def maximumScore(self, nums: List[int], k: int) -> int:
NEG = float('-inf')
def best(a: List[int]) -> int:
# at most k disjoint pairs (any order), each worth |a[p] - a[q]|
m = min(k, len(a) // 2)
idle = [NEG] * (m + 1) # j pairs finished
plus = [NEG] * (m + 1) # j-th pair open, holding +a[p]
minus = [NEG] * (m + 1) # j-th pair open, holding -a[p]
idle[0] = 0
for x in a:
for j in range(m, -1, -1):
done = max(idle[j], plus[j] - x, minus[j] + x)
if j > 0:
plus[j] = max(plus[j], idle[j - 1] + x)
minus[j] = max(minus[j], idle[j - 1] - x)
idle[j] = done
return max(idle)
# Start at a maximum: then the only piece that wraps can be read in one of the two directions.
i = nums.index(max(nums))
a = nums[i:] + nums[:i]
return max(best(a), best([a[0]] + a[:0:-1]))