<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <code>n</code> positive integers.</p>

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

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

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

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<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,<u><strong>1</strong></u>,<u><strong>3</strong></u>,<u><strong>1</strong></u>].
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 class="example">Example 2:</strong></p>

<pre>
<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,<u><strong>1</strong></u>,2,<u><strong>1</strong></u>].
The number of operations required in this case is 2.
Note that the array cannot be converted to [<u><strong>2</strong></u>,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.
</pre>

<p>&nbsp;</p>
<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>

# Idea
# build two Counters, one for each alternating series
# find the most common value for each.
# The number of change operations should be the number of values that don't match that value

# special case: most common value is the same across both series
# solution: figure out number of flips for _second_ most common value of each

from collections import Counter

class Solution:
    def minimumOperations(self, nums: List[int]) -> int:
        if len(nums) < 2:
            return 0

        evenSeriesCounter = buildSeriesCounter(nums, 0)
        oddSeriesCounter = buildSeriesCounter(nums, 1)
        print(f"even series: {evenSeriesCounter}")
        print(f"odd series: {oddSeriesCounter}")

        evenSeriesOps, oddSeriesOps = 0,0

        evenSeriesNum, evenSeriesNumCount = evenSeriesCounter.most_common(1)[0]
        evenSeriesOps = evenSeriesCounter.total() - evenSeriesNumCount

        oddSeriesNum, oddSeriesNumCount = oddSeriesCounter.most_common(1)[0]
        oddSeriesOps = oddSeriesCounter.total() - oddSeriesNumCount
        print(f"initial nums, evenSeriesNum: {evenSeriesNum}, oddSeriesNum: {oddSeriesNum}")

        if evenSeriesNum != oddSeriesNum:
            return evenSeriesOps + oddSeriesOps


        if len(oddSeriesCounter) == 1:
            oddSeriesNumCount = 0
        else:
            oddSeriesNumCount = oddSeriesCounter.most_common(2)[1][1]
        totalIfOddSwitched = evenSeriesOps + (oddSeriesCounter.total() - oddSeriesNumCount)

        if len(evenSeriesCounter) == 1:
            evenSeriesNumCount = 0
        else:
            evenSeriesNumCount = evenSeriesCounter.most_common(2)[1][1]
        totalIfEvenSwitched = oddSeriesOps + (evenSeriesCounter.total() - evenSeriesNumCount)

        return min(totalIfOddSwitched, totalIfEvenSwitched)

def buildSeriesCounter(nums: List[int], offset: int):
    return Counter([num for i,num in enumerate(nums) if (i+offset)%2 == 0])

def getNumberOfOpsToMatch(num: int, seriesCounter) -> int:
    return seriesCounter.total() - seriesCounter.get(num, 0)
986