From 3b77f0f31e51d8930b1d66deee021ff7b2d84908 Mon Sep 17 00:00:00 2001 From: CodersAcademy006 <104912634+CodersAcademy006@users.noreply.github.com> Date: Thu, 18 Jul 2024 13:29:35 +0530 Subject: [PATCH] Update Solution.java 3 Error's resolved --- .../Solution.java | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/solution/1300-1399/1330.Reverse Subarray To Maximize Array Value/Solution.java b/solution/1300-1399/1330.Reverse Subarray To Maximize Array Value/Solution.java index 8d3cec383ebda..d674750d2c1bf 100644 --- a/solution/1300-1399/1330.Reverse Subarray To Maximize Array Value/Solution.java +++ b/solution/1300-1399/1330.Reverse Subarray To Maximize Array Value/Solution.java @@ -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]); } -} \ No newline at end of file + 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; + } +}