4087. Maximum Subarray Sum of Fixed Length After at Most K Swaps π
DifficultyMedium
Description
You are given an integer array nums and two integers length and k.
You may perform at most k swap operations on nums.
In one swap operation, you may choose two distinct indices i and j and swap nums[i] and nums[j].
Return the maximum possible sum of a subarray of length exactly length after performing the swaps.
Example 1:
Input: nums = [1,-1,0,2], length = 2, k = 1
Output: 3
Explanation:
- Swap the elements at indices 1 and 3, resulting in
[1, 2, 0, -1]. - The subarray
[1, 2]has length 2 and sum 3. - No subarray of length 2 can have a larger sum, since 1 and 2 are the two largest elements.
Example 2:
Input: nums = [4,3,2,4], length = 3, k = 2
Output: 11
Explanation:
- Swap the elements at indices 2 and 3, resulting in
[4, 3, 4, 2]. - The subarray
[4, 3, 4]has length 3 and sum 11. - This is the sum of the three largest elements, so no larger answer is possible. Only one swap is needed.
Example 3:
Input: nums = [-1,-2], length = 2, k = 0
Output: -3
Explanation:
No swaps are allowed. The only subarray of length 2 is [-1, -2], whose sum is -3.
Constraints:
1 <= nums.length <= 300-109 <= nums[i] <= 1091 <= length <= nums.length0 <= k <= nums.length
Solutions
Solution 1: Enumerate Windows and Swap Greedily
Thinking
\(n\) is at most \(300\), so every window of length \(\textit{length}\) can be evaluated directly. Choosing which indices to swap inside a window grows exponentially with \(k\).
The bottleneck is the window boundary. Swapping two elements that are both inside the window, or both outside it, does not change that window's sum. Only an exchange across the boundary does, so the number of useful swaps is at most \(\min(k,\textit{length},n-\textit{length})\).
The sum grows only when the incoming value is larger than the outgoing one, so a larger outside value should replace a smaller inside value. After the inside values are sorted in ascending order and the outside values in descending order, the gain of the \(j\)-th swap is non-increasing. Pair from the smallest inside value and stop at the first non-positive gain. The best sum among all windows is the answer.
Fix the window starting at index \(i\). Write the inside values in non-decreasing order \(a_0 \le a_1 \le \cdots\) and the outside values in non-increasing order \(b_0 \ge b_1 \ge \cdots\). After \(t\) swaps, the change in the window sum equals the sum of the incoming values minus the sum of the outgoing values. For a fixed \(t\), the optimal choice brings in the \(t\) largest outside values and sends out the \(t\) smallest inside values, for a change of \(\sum_{j=0}^{t-1}(b_j-a_j)\). If an incoming value is outside that set, replacing it with a larger unused outside value cannot decrease the sum. If an outgoing value is not one of those \(t\) smallest inside values, replacing it with a smaller inside value that has not yet been sent out cannot decrease the sum either.
Because \(b_j-a_j\) is non-increasing, the positive terms form a prefix. The number of swaps actually used is the smaller of that prefix length and \(\min(k,\textit{length},n-\textit{length})\). Adding the corresponding gains to the original window sum gives the best sum for that starting index, and the answer is the maximum over all starting indices.
The time complexity is \(O(n^2 \log n)\) and the space complexity is \(O(n)\), where \(n\) is the length of \(\textit{nums}\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |