{"id":6135,"date":"2020-01-26T09:44:37","date_gmt":"2020-01-26T17:44:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6135"},"modified":"2020-01-26T09:45:00","modified_gmt":"2020-01-26T17:45:00","slug":"leetcode-1330-reverse-subarray-to-maximize-array-value","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1330-reverse-subarray-to-maximize-array-value\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1330. Reverse Subarray To Maximize Array Value"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>. The&nbsp;<em>value<\/em>&nbsp;of this array is defined as the sum of&nbsp;<code>|nums[i]-nums[i+1]|<\/code>&nbsp;for all&nbsp;<code>0 &lt;= i &lt; nums.length-1<\/code>.<\/p>\n\n\n\n<p>You are allowed to select any subarray of the given array and reverse it. You can perform this operation&nbsp;<strong>only once<\/strong>.<\/p>\n\n\n\n<p>Find maximum possible value of the final array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,3,1,5,4]\n<strong>Output:<\/strong> 10\n<strong>Explanation: <\/strong>By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,4,9,24,2,1,10]\n<strong>Output:<\/strong> 68\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 3*10^4<\/code><\/li><li><code>-10^5 &lt;= nums[i] &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxValueAfterReverse(vector<int>& nums) {\n    const int n = nums.size();\n    int sum = 0;\n    int gain = 0;    \n    int hi = INT_MIN;\n    int lo = INT_MAX;\n    for (int i = 0; i < n - 1; ++i) {\n      int n1 = nums[i];\n      int n2 = nums[i + 1];\n      sum += abs(n1 - n2);\n      gain = max({gain, \n                 abs(nums[0] - n2) - abs(n1 - n2),\n                 abs(nums[n - 1] - n1) - abs(n1 - n2)});\n      hi = max(hi, min(n1, n2));\n      lo = min(lo, max(n1, n2));\n    }\n    return sum + max(gain, (hi - lo) * 2);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums. The&nbsp;value&nbsp;of this array is defined as the sum of&nbsp;|nums[i]-nums[i+1]|&nbsp;for all&nbsp;0 &lt;= i &lt; nums.length-1. You are allowed to select&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[20,88,376,246],"class_list":["post-6135","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-array","tag-greedy","tag-on","tag-reverse","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6135"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6135\/revisions"}],"predecessor-version":[{"id":6137,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6135\/revisions\/6137"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}