#1330

Reverse Subarray To Maximize Array Value

master · 1605 · lc hard +32 · verified · 43.9% accepted · 493 likes · top 26%

Description

For an integer array nums, its value is the sum of |nums[i] - nums[i + 1]| over all consecutive index pairs. You may reverse any one contiguous subarray exactly once. Return the maximum value achievable.

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

Code

1
2
3