Title: #1330 - Reverse Subarray To Maximize Array Value - LeetCode JavaScript Solutions
Open Graph Title: #1330 - Reverse Subarray To Maximize Array Value - LeetCode JavaScript Solutions
X Title: #1330 - Reverse Subarray To Maximize Array Value - LeetCode JavaScript Solutions
Description: 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.le...
Open Graph Description: 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.le...
X Description: 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.le...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/reverse-subarray-to-maximize-array-value
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/reverse-subarray-to-maximize-array-value","name":"#1330 - Reverse Subarray To Maximize Array Value - LeetCode JavaScript Solutions","description":"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.le...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxValueAfterReverse = function(nums) {\n let baseSum = 0;\n let maxGain = 0;\n let minPair = Infinity;\n let maxPair = -Infinity;\n const n = nums.length;\n\n for (let i = 0; i < n - 1; i++) {\n baseSum += Math.abs(nums[i] - nums[i + 1]);\n minPair = Math.min(minPair, Math.max(nums[i], nums[i + 1]));\n maxPair = Math.max(maxPair, Math.min(nums[i], nums[i + 1]));\n }\n\n maxGain = Math.max(0, 2 * (maxPair - minPair));\n\n for (let i = 0; i < n - 1; i++) {\n const left = i > 0 ? Math.abs(nums[0] - nums[i + 1]) - Math.abs(nums[i] - nums[i + 1]) : 0;\n const right = i < n - 2 ? Math.abs(nums[n - 1] - nums[i]) - Math.abs(nums[i] - nums[i + 1]) : 0;\n maxGain = Math.max(maxGain, left, right);\n }\n\n return baseSum + maxGain;\n};","keywords":"LeetCode 1330, #1330 - Reverse Subarray To Maximize Array Value, Hard, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/reverse-subarray-to-maximize-array-value |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/reverse-subarray-to-maximize-array-value/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1330-reverse-subarray-to-maximize-array-value.js |
| Array | https://leetcodejavascript.com/tags/array |
| Math | https://leetcodejavascript.com/tags/math |
| Greedy | https://leetcodejavascript.com/tags/greedy |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow