|values[0] − values[1]| + |values[1] − values[2]| + … + |values[n−2] − values[n−1]|.
You are given an integer array values. You may, **at most once**, pick a contiguous subarray and reverse it.
Return the largest roughness you can achieve.values=[1, 5, 6, 2] → 13
The roughness is now 4 + 1 + 4 = 9. Reverse the opening [1, 5] to get [5, 1, 6, 2], worth 4 + 5 + 4 = 13.
values=[1, 2, 3, 4] → 5
Reverse [1, 2, 3]: the array [3, 2, 1, 4] gives 1 + 1 + 3 = 5.
values=[5, 5, 5] → 0
All numbers are equal, so no reversal can lift the roughness above zero.
values=[1, 5, 6, 2]
13
Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.