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:
nums into [2, 3] and [3, 1] (wrapped around).[2, 3] is max(2, 3) - min(2, 3) = 3 - 2 = 1.[3, 1] is max(3, 1) - min(3, 1) = 3 - 1 = 2.1 + 2 = 3.Example 2:
Input: nums = [1,2,3,3], k = 1
Output: 2
Explanation:
nums into [1, 2, 3, 3].[1, 2, 3, 3] is max(1, 2, 3, 3) - min(1, 2, 3, 3) = 3 - 1 = 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] <= 1091 <= k <= nums.length2 * k picks (each partition can supply a + and a -), so the problem becomes choosing which elements are + or -.nums[i]) and min (-nums[i]) contributions; selecting an element as a max adds +nums[i], and selecting it as a min adds -nums[i].(picks, balance) where picks is the total number of + and - chosen so far (<= 2 * k), and balance represents the difference between the counts of + and - currently; at each i, you conceptually take +, take -, or skip.balance to the range [0, 2]. You can show that such a balance is always achievable.[1,3,4] 2
[1,1000000000,1000000000] 3
[1] 1