Reverse Subarray To Maximize Array Value Problem
Description
LeetCode Problem 1330.
You are given an integer array nums. The value of this array is defined as the sum of |nums[i] - nums[i + 1]| for all 0 <= i < nums.length - 1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
2 <= nums.length <= 3 * 10^4-10^5 <= nums[i] <= 10^5- The answer is guaranteed to fit in a 32-bit integer.
Solution
Approach: Only the Edges at the Ends Change
Call each pair of neighbours (nums[i], nums[i + 1]) an edge, worth |nums[i] - nums[i + 1]|. The value of the array is the sum of all edges, total. Reversing a subarray keeps every edge inside it (in the opposite order), so the only edges that change are the ones crossing its two ends. We look for the reversal with the biggest gain, which is never negative, since we may also reverse a single element and change nothing.
A prefix or a suffix. Reversing nums[0..i] changes only the edge (nums[i], nums[i + 1]): it becomes (nums[0], nums[i + 1]). The gain is |nums[0] - nums[i + 1]| - |nums[i] - nums[i + 1]|. In the same way, reversing nums[i + 1..n - 1] turns that edge into (nums[i], nums[n - 1]). Trying every i covers all these cases.
A subarray in the middle. Now two edges change. Say the left edge is (a, b) and the right edge is (c, d), with the subarray running from b to c. After the reversal, the edges are (a, c) and (b, d). Think of each edge as the interval between its two values:
- If the two intervals overlap, the new edges are never longer than the old ones: no gain.
- If they do not overlap, say the interval of one edge lies entirely below the other, then both new edges go from the lower interval to the higher one. The gain works out to exactly
2 * (low of the higher edge - high of the lower edge): twice the gap between the two intervals.
So the best middle reversal uses the edge with the largest min and the edge with the smallest max, and gains 2 * (max of mins - min of maxes). If both come from the same edge, or the intervals overlap, this value is not positive, so it never overstates the gain. Which edge is on the left does not matter, since the formula is symmetric.
The answer is total plus the best gain among all these cases.
Complexity: O(n) time and O(1) extra space.
class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
n = len(nums)
total = sum(abs(nums[i] - nums[i + 1]) for i in range(n - 1))
gain = 0
# A reversed prefix or suffix changes only the one edge at its inner end.
for i in range(n - 1):
x, y = nums[i], nums[i + 1]
gain = max(gain, abs(nums[0] - y) - abs(x - y)) # reverse nums[0..i]
gain = max(gain, abs(nums[-1] - x) - abs(x - y)) # reverse nums[i+1..n-1]
# A reversal inside swaps two edges; it pays off by twice the gap between
# the lower edge's top (its max) and the higher edge's bottom (its min).
highest_low = max(min(nums[i], nums[i + 1]) for i in range(n - 1))
lowest_high = min(max(nums[i], nums[i + 1]) for i in range(n - 1))
gain = max(gain, 2 * (highest_low - lowest_high))
return total + gain