You are given an integer array nums of length n and an integer k.
You must select exactlykdistinct non-empty subarraysnums[l..r] of nums. Subarrays may overlap, but the exact same subarray (same l and r) cannot be chosen more than once.
The value of a subarray nums[l..r] is defined as: max(nums[l..r]) - min(nums[l..r]).
The total value is the sum of the values of all chosen subarrays.
Return the maximum possible total value you can achieve.
Β
Example 1:
Input:nums = [1,3,2], k = 2
Output:4
Explanation:
One optimal approach is:
Choose nums[0..1] = [1, 3]. The maximum is 3 and the minimum is 1, giving a value of 3 - 1 = 2.
Choose nums[0..2] = [1, 3, 2]. The maximum is still 3 and the minimum is still 1, so the value is also 3 - 1 = 2.
Adding these gives 2 + 2 = 4.
Example 2:
Input:nums = [4,2,5,1], k = 3
Output:12
Explanation:
One optimal approach is:
Choose nums[0..3] = [4, 2, 5, 1]. The maximum is 5 and the minimum is 1, giving a value of 5 - 1 = 4.
Choose nums[1..3] = [2, 5, 1]. The maximum is 5 and the minimum is 1, so the value is also 4.
Choose nums[2..3] = [5, 1]. The maximum is 5 and the minimum is 1, so the value is again 4.
Adding these gives 4 + 4 + 4 = 12.
Β
Constraints:
1 <= n == nums.length <= 5 * 10βββββββ4
Consider enumerating the left boundary \(l\) of the subarray. As the right boundary \(r\) moves to the right, the value of the subarray \(\textit{nums}[l..r]\) increases monotonically. This is because the maximum value within the interval can only increase (or remain unchanged), while the minimum value can only decrease (or remain unchanged). Thus, their difference, \(\max(\textit{nums}[l..r]) - \min(\textit{nums}[l..r])\), possesses a monotonically non-decreasing property.
This implies that for each fixed left endpoint \(l\), we have a monotonically increasing sequence of length \(n - l\), where the \(i\)-th element represents the value of \(\textit{nums}[l..l+i]\). The problem then transforms into: Given \(n\) monotonically increasing sequences, find the sum of the top \(k\) largest elements across all sequences.
Since the last element of each sequence (i.e., when \(r = n - 1\)) is the maximum value of that sequence, we can utilize a max-heap (priority queue) to filter them efficiently:
Initialization: Push the last element of each sequence (where \(r = n - 1\)) along with its coordinates, represented as \((val, l, n - 1)\), into the max-heap.
Iterative Greedy Choice: Repeat the operation \(k\) times. In each iteration, pop the top element \((val, l, r)\) from the heap and accumulate \(val\) into the total answer. If \(r > l\), it indicates that there are still smaller, next-largest values remaining in this sequence. We then compute the value of the previous element in the same sequence, \((l, r - 1)\), and push it back into the heap.
Range Minimum/Maximum Query Optimization: To query both the maximum and minimum values of any subarray \([l, r]\) in \(\mathcal{O}(1)\) time, we can precompute a Sparse Table (ST).
The time complexity is \(\mathcal{O}(n \log n + k \log n)\), and the space Complexity is \(\mathcal{O}(n \log n)\), where \(n\) is the length of the array \(\textit{nums}\).