<?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>alternating Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/alternating/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/alternating/</link>
	<description></description>
	<lastBuildDate>Sun, 13 Feb 2022 13:45:16 +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>alternating Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/alternating/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2170. Minimum Operations to Make the Array Alternating</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-2170-minimum-operations-to-make-the-array-alternating/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-2170-minimum-operations-to-make-the-array-alternating/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 Feb 2022 13:44:11 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[alternating]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9496</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;array&#160;nums&#160;consisting of&#160;n&#160;positive integers. The array&#160;nums&#160;is called&#160;alternating&#160;if: nums[i - 2] == nums[i], where&#160;2 &#60;= i &#60;= n - 1. nums[i - 1] !=&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-2170-minimum-operations-to-make-the-array-alternating/">花花酱 LeetCode 2170. Minimum Operations to Make the Array Alternating</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>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;array&nbsp;<code>nums</code>&nbsp;consisting of&nbsp;<code>n</code>&nbsp;positive integers.</p>



<p>The array&nbsp;<code>nums</code>&nbsp;is called&nbsp;<strong>alternating</strong>&nbsp;if:</p>



<ul><li><code>nums[i - 2] == nums[i]</code>, where&nbsp;<code>2 &lt;= i &lt;= n - 1</code>.</li><li><code>nums[i - 1] != nums[i]</code>, where&nbsp;<code>1 &lt;= i &lt;= n - 1</code>.</li></ul>



<p>In one&nbsp;<strong>operation</strong>, you can choose an index&nbsp;<code>i</code>&nbsp;and&nbsp;<strong>change</strong>&nbsp;<code>nums[i]</code>&nbsp;into&nbsp;<strong>any</strong>&nbsp;positive integer.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum number of operations</strong>&nbsp;required to make the array alternating</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,1,3,2,4,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
One way to make the array alternating is by converting it to [3,1,3,<strong>1</strong>,<strong>3</strong>,<strong>1</strong>].
The number of operations required in this case is 3.
It can be proven that it is not possible to make the array alternating in less than 3 operations. 
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,2,2,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
One way to make the array alternating is by converting it to [1,2,<strong>1</strong>,2,<strong>1</strong>].
The number of operations required in this case is 2.
Note that the array cannot be converted to [<strong>2</strong>,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.
</pre>



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



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



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



<p>Count and sort the frequency of numbers at odd and even positions.</p>



<p>Case 1: top frequency numbers are different, change the rest of numbers to them respectively.<br>Case 2: top frequency numbers are the same, compare top 1 odd + top 2 even vs top 2 even + top 1 odd.</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minimumOperations(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    if (n == 1) return 0;
    unordered_map&lt;int, int&gt; odd, even;
    for (int i = 0; i &lt; n; ++i)
      ((i &amp; 1 ? odd : even)[nums[i]])++;
    vector&lt;pair&lt;int, int&gt;&gt; o, e;
    for (const auto [k, v] : odd)
      o.emplace_back(v, k);
    for (const auto [k, v] : even)
      e.emplace_back(v, k);
    sort(rbegin(o), rend(o));
    sort(rbegin(e), rend(e));        
    if (o[0].second != e[0].second) 
      return n - o[0].first - e[0].first;    
    int mo = o[0].first + (e.size() &gt; 1 ? e[1].first : 0);
    int me = e[0].first + (o.size() &gt; 1 ? o[1].first : 0);
    return n - max(mo, me);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-2170-minimum-operations-to-make-the-array-alternating/">花花酱 LeetCode 2170. Minimum Operations to Make the Array Alternating</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-2170-minimum-operations-to-make-the-array-alternating/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1864. Minimum Number of Swaps to Make the Binary String Alternating</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1864-minimum-number-of-swaps-to-make-the-binary-string-alternating/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1864-minimum-number-of-swaps-to-make-the-binary-string-alternating/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 05 Aug 2021 05:29:11 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[alternating]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[swap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8467</guid>

					<description><![CDATA[<p>Given a binary string&#160;s, return&#160;the&#160;minimum&#160;number of character swaps to make it&#160;alternating, or&#160;-1&#160;if it is impossible. The string is called&#160;alternating&#160;if no two adjacent characters are equal.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1864-minimum-number-of-swaps-to-make-the-binary-string-alternating/">花花酱 LeetCode 1864. Minimum Number of Swaps to Make the Binary String Alternating</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 a binary string&nbsp;<code>s</code>, return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;number of character swaps to make it&nbsp;<strong>alternating</strong>, or&nbsp;</em><code>-1</code><em>&nbsp;if it is impossible.</em></p>



<p>The string is called&nbsp;<strong>alternating</strong>&nbsp;if no two adjacent characters are equal. For example, the strings&nbsp;<code>"010"</code>&nbsp;and&nbsp;<code>"1010"</code>&nbsp;are alternating, while the string&nbsp;<code>"0100"</code>&nbsp;is not.</p>



<p>Any two characters may be swapped, even if they are&nbsp;<strong>not adjacent</strong>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "111000"
<strong>Output:</strong> 1
<strong>Explanation:</strong> Swap positions 1 and 4: "111000" -&gt; "101010"
The string is now alternating.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "010"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string is already alternating, no swaps are needed.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1110"
<strong>Output:</strong> -1
</pre>



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



<ul><li><code>1 &lt;= s.length &lt;= 1000</code></li><li><code>s[i]</code>&nbsp;is either&nbsp;<code>'0'</code>&nbsp;or&nbsp;<code>'1'</code>.</li></ul>



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



<p>Two passes, make the string starts with &#8216;0&#8217; or &#8216;1&#8217;, count how many 0/1 swaps needed. 0/1 swaps must equal otherwise it&#8217;s impossible to swap.</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 minSwaps(string s) {    
    auto count = [&amp;](int m) {
      vector&lt;int&gt; swaps(2);
      for (int i = 0; i &lt; s.length(); ++i, m ^= 1)
        if (s[i] - '0' != m)
          ++swaps[s[i] - '0'];
      return swaps[0] == swaps[1] ? swaps[0] : INT_MAX;
    };
    
    int ans = min(count(0), count(1));
    return ans != INT_MAX ? ans : -1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1864-minimum-number-of-swaps-to-make-the-binary-string-alternating/">花花酱 LeetCode 1864. Minimum Number of Swaps to Make the Binary String Alternating</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-1864-minimum-number-of-swaps-to-make-the-binary-string-alternating/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
