Maximize Cyclic Partition Score

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
Inputnums = [1,2,3,3], k = 2
Output3
Partition nums into [2, 3] and [3, 1] wrapped around, whose ranges are 1 and 2 for a total score of 3.
Example 2
Inputnums = [1,2,3,3], k = 1
Output2
Partition nums into [1, 2, 3, 3], whose range is 3 - 1 = 2, so the score is 2.

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 10^9
  • 1 <= k <= nums.length

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate