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

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

        counts = [Counter(), Counter()]
        for i in range(n):
            counts[i%2][nums[i]] += 1

        arr1 = [[-cnt, num] for num, cnt in counts[0].items()]
        arr2 = [[-cnt, num] for num, cnt in counts[1].items()]
        heapq.heapify(arr1)
        heapq.heapify(arr2)

        cnt1, num1 = heapq.heappop(arr1)
        cnt2, num2 = heapq.heappop(arr2)
        if num1 != num2:
            return n+cnt1+cnt2
        else:
            cnt1_2nd = arr1[0][0] if arr1 else 0
            cnt2_2nd = arr2[0][0] if arr2 else 0
            return n + min(cnt2+cnt1_2nd, cnt1+cnt2_2nd)

# # Intuition

# only top 2 frequency num matter.

# find top 2 frequency element in both odd and even

# odd: x1, x2 where # of x1 element > # of x2
# even: y1, y1 where # of y1 > # of y2

# if x1 != y1:
#     return n - (# of x1) - (# of y1)
# else:
#     return min(n - (# of x1 + # of y2), n - (# of x2 + # of y1))
990