-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1330-reverse-subarray-to-maximize-array-value.js
41 lines (36 loc) · 1.26 KB
/
1330-reverse-subarray-to-maximize-array-value.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 1330. Reverse Subarray To Maximize Array Value
* https://leetcode.com/problems/reverse-subarray-to-maximize-array-value/
* Difficulty: Hard
*
* 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.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var maxValueAfterReverse = function(nums) {
let baseSum = 0;
let maxGain = 0;
let minPair = Infinity;
let maxPair = -Infinity;
const n = nums.length;
for (let i = 0; i < n - 1; i++) {
baseSum += Math.abs(nums[i] - nums[i + 1]);
minPair = Math.min(minPair, Math.max(nums[i], nums[i + 1]));
maxPair = Math.max(maxPair, Math.min(nums[i], nums[i + 1]));
}
maxGain = Math.max(0, 2 * (maxPair - minPair));
for (let i = 0; i < n - 1; i++) {
const left = i > 0 ? Math.abs(nums[0] - nums[i + 1]) - Math.abs(nums[i] - nums[i + 1]) : 0;
const right = i < n - 2 ? Math.abs(nums[n - 1] - nums[i]) - Math.abs(nums[i] - nums[i + 1]) : 0;
maxGain = Math.max(maxGain, left, right);
}
return baseSum + maxGain;
};