<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>local minimum Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/local-minimum/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/local-minimum/</link>
	<description></description>
	<lastBuildDate>Tue, 06 Aug 2019 16:33:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>local minimum Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/local-minimum/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1144. Decrease Elements To Make Array Zigzag</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1144-decrease-elements-to-make-array-zigzag/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1144-decrease-elements-to-make-array-zigzag/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 04 Aug 2019 17:20:03 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[local minimum]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5381</guid>

					<description><![CDATA[<p>Given an array&#160;nums&#160;of integers, a&#160;move&#160;consists of choosing any element and&#160;decreasing it by 1. An array&#160;A&#160;is a&#160;zigzag array&#160;if either: Every even-indexed element is greater than adjacent&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1144-decrease-elements-to-make-array-zigzag/">花花酱 LeetCode 1144. Decrease Elements To Make Array Zigzag</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p>An array&nbsp;<code>A</code>&nbsp;is a&nbsp;<em>zigzag array</em>&nbsp;if either:</p>



<ul><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>



<p>Return the minimum number of moves to transform the given array&nbsp;<code>nums</code>&nbsp;into a zigzag array.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can decrease 2 to 0 or 3 to 1.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [9,6,1,6,2]
<strong>Output:</strong> 4
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= nums.length &lt;= 1000</code></li><li><code>1 &lt;= nums[i] &lt;= 1000</code></li></ul>



<h2><strong>Solution: Greedy</strong></h2>



<p>One pass, making each element local minimum. </p>



<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>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int movesToMakeZigzag(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    vector&lt;int&gt; moves(2);    
    for (int i = 0; i &lt; n; i++) {
      int l = i == 0 ? INT_MAX : nums[i - 1];
      int r = i == n - 1 ? INT_MAX : nums[i + 1];
      moves[i % 2] += max(0, nums[i] - min(l, r) + 1);
    }    
    return min(moves[0], moves[1]);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1144-decrease-elements-to-make-array-zigzag/">花花酱 LeetCode 1144. Decrease Elements To Make Array Zigzag</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/greedy/leetcode-1144-decrease-elements-to-make-array-zigzag/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
