{"id":5381,"date":"2019-08-04T10:20:03","date_gmt":"2019-08-04T17:20:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5381"},"modified":"2019-08-06T09:33:21","modified_gmt":"2019-08-06T16:33:21","slug":"leetcode-1144-decrease-elements-to-make-array-zigzag","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1144-decrease-elements-to-make-array-zigzag\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1144. Decrease Elements To Make Array Zigzag"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>nums<\/code>&nbsp;of integers, a&nbsp;<em>move<\/em>&nbsp;consists of choosing any element and&nbsp;<strong>decreasing it by 1<\/strong>.<\/p>\n\n\n\n<p>An array&nbsp;<code>A<\/code>&nbsp;is a&nbsp;<em>zigzag array<\/em>&nbsp;if either:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Every even-indexed element is greater than adjacent elements, ie.&nbsp;<code>A[0] &gt; A[1] &lt; A[2] &gt; A[3] &lt; A[4] &gt; ...<\/code><\/li><li>OR, every odd-indexed element is greater than adjacent elements, ie.&nbsp;<code>A[0] &lt; A[1] &gt; A[2] &lt; A[3] &gt; A[4] &lt; ...<\/code><\/li><\/ul>\n\n\n\n<p>Return the minimum number of moves to transform the given array&nbsp;<code>nums<\/code>&nbsp;into a zigzag 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 = [1,2,3]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> We can decrease 2 to 0 or 3 to 1.\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 = [9,6,1,6,2]\n<strong>Output:<\/strong> 4\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;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>One pass, making each element local minimum. <\/p>\n\n\n\n<p>[9,6,1,6,2] <br>i = 0, [inf, 9, 6], 9 =&gt; 5, even cost 4<br>i = 1, [9, 6, 1], 6 =&gt; 0, odd cost 6<br>i = 2, [6, 1, 6], 1 =&gt; 1, even cost 0<br>i = 3, [1, 6, 2], 6 =&gt; 0, odd cost 12<br>i = 4, [6, 2, inf], 2 =&gt; 2, even cost 0<br>total even cost 4, new array =&gt; [5, 6, 1, 6, 2]<br>total odd cost 18, new array =&gt; [9, 0, 1, 0, 2]<\/p>\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 movesToMakeZigzag(vector<int>& nums) {\n    const int n = nums.size();\n    vector<int> moves(2);    \n    for (int i = 0; i < n; i++) {\n      int l = i == 0 ? INT_MAX : nums[i - 1];\n      int r = i == n - 1 ? INT_MAX : nums[i + 1];\n      moves[i % 2] += max(0, nums[i] - min(l, r) + 1);\n    }    \n    return min(moves[0], moves[1]);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;nums&nbsp;of integers, a&nbsp;move&nbsp;consists of choosing any element and&nbsp;decreasing it by 1. An array&nbsp;A&nbsp;is a&nbsp;zigzag array&nbsp;if either: Every even-indexed element is greater than adjacent&#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":[88,487,177],"class_list":["post-5381","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-local-minimum","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5381","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=5381"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5381\/revisions"}],"predecessor-version":[{"id":5396,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5381\/revisions\/5396"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}