forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
29 additions
and
28 deletions.
There are no files selected for viewing
57 changes: 29 additions & 28 deletions
57
solution/1300-1399/1330.Reverse Subarray To Maximize Array Value/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,31 @@ | ||
class Solution { | ||
public int maxValueAfterReverse(int[] nums) { | ||
int n = nums.length; | ||
int s = 0; | ||
for (int i = 0; i < n - 1; ++i) { | ||
s += Math.abs(nums[i] - nums[i + 1]); | ||
} | ||
int ans = s; | ||
for (int i = 0; i < n - 1; ++i) { | ||
ans = Math.max( | ||
ans, s + Math.abs(nums[0] - nums[i + 1]) - Math.abs(nums[i] - nums[i + 1])); | ||
ans = Math.max( | ||
ans, s + Math.abs(nums[n - 1] - nums[i]) - Math.abs(nums[i] - nums[i + 1])); | ||
} | ||
int[] dirs = {1, -1, -1, 1, 1}; | ||
final int inf = 1 << 30; | ||
for (int k = 0; k < 4; ++k) { | ||
int k1 = dirs[k], k2 = dirs[k + 1]; | ||
int mx = -inf, mi = inf; | ||
for (int i = 0; i < n - 1; ++i) { | ||
int a = k1 * nums[i] + k2 * nums[i + 1]; | ||
int b = Math.abs(nums[i] - nums[i + 1]); | ||
mx = Math.max(mx, a - b); | ||
mi = Math.min(mi, a + b); | ||
} | ||
ans = Math.max(ans, s + Math.max(0, mx - mi)); | ||
} | ||
return ans; | ||
|
||
public int maxValueAfterReverse(int[] nums) { | ||
int n = nums.length; | ||
int s = 0; | ||
for (int i = 0; i < n - 1; ++i) { | ||
s += Math.abs(nums[i] - nums[i + 1]); | ||
} | ||
} | ||
int ans = s; | ||
for (int i = 0; i < n - 1; ++i) { | ||
ans = Math.max( | ||
ans, s + Math.abs(nums[0] - nums[i + 1]) - Math.abs(nums[i] - nums[i + 1])); | ||
ans = Math.max( | ||
ans, s + Math.abs(nums[n - 1] - nums[i]) - Math.abs(nums[i] - nums[i + 1])); | ||
} | ||
int[] dirs = {1, -1, -1, 1, 1}; | ||
final int inf = 1 << 30; | ||
for (int k = 0; k < 4; ++k) { | ||
int k1 = dirs[k], k2 = dirs[k + 1]; | ||
int mx = -inf, mi = inf; | ||
for (int i = 0; i < n - 1; ++i) { | ||
int a = k1 * nums[i] + k2 * nums[i + 1]; | ||
int b = Math.abs(nums[i] - nums[i + 1]); | ||
mx = Math.max(mx, a - b); | ||
mi = Math.min(mi, a + b); | ||
} | ||
ans = Math.max(ans, s + Math.max(0, mx - mi)); | ||
} | ||
return ans; | ||
} | ||
} |