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:
1 <= nums.length <= 3*10^4
-10^5 <= nums[i] <= 10^5
class Solution:
    def maxValueAfterReverse(self, nums: List[int]) -> int:
        # 三种情况，从头到中间某个地方进行reverse；
        # 从中间到最后进行reverse，中间的某一段进行reverse
        total, res, min2, max2 = 0, 0, float('inf'), float('-inf')
        for a, b in zip(nums[:-1], nums[1:]):
            total += abs(a - b)
            res = max(res, abs(nums[0] - b) - abs(a - b))
            res = max(res, abs(nums[-1] - a) - abs(a - b))
            min2, max2 = min(min2, max(a, b)), max(max2, min(a, b))
        return total + max(res, (max2 - min2) * 2)
class Solution:
    def maxValueAfterReverse(self, nums: List[int]) -> int:
        # 三种情况，从头到中间某个地方进行reverse；
        # 从中间到最后进行reverse，中间的某一段进行reverse
        total, res, min2, max2 = 0, 0, float('inf'), float('-inf')
        for a, b in zip(nums[:-1], nums[1:]):
            total += abs(a - b)
            res = max(res, abs(nums[0] - b) - abs(a - b))
            res = max(res, abs(nums[-1] - a) - abs(a - b))
            min2, max2 = min(min2, max(a, b)), max(max2, min(a, b))
        return total + max(res, (max2 - min2) * 2)