<p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>

<p>You can perform two types of operations on any element of the array any number of times:</p>

<ul>
	<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.

	<ul>
		<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
	</ul>
	</li>
	<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
	<ul>
		<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
	</ul>
	</li>
</ul>

<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>

<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>2 &lt;= n &lt;= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

from sortedcontainers import SortedList
class Solution:
    def minimumDeviation(self, nums: List[int]) -> int:
        # even / 2 could be odd or even
        # odd * 2 always even
        # deviation is just max - min
        # two sortedcontainers solution? acting max and min trees
        # or something with sorting
        # note that we cant increase the min elm if its even
        # cant decrease the max elm if its odd

        # sol: instead of looking at it from two dimensions, we can first minimize everything then
        # only look at increasing the odd numbers


        # maxheap sol
        maxheap = []
        min_n = float('inf')
        ans = float('inf')

        for n in nums:
            if n % 2 == 1:
                n = n * 2
            
            min_n = min(min_n, n)
            heapq.heappush(maxheap, -n)

        while maxheap[0] % 2 == 0:
            elm = -1 * heapq.heappop(maxheap)
            ans = min(ans, elm - min_n)
            min_n = min(min_n, elm//2)
            print(min_n)
            heapq.heappush(maxheap, -elm//2)
        
        return min(ans, -1 * maxheap[0] - min_n)


'''
mh = []
        max_n = float('-inf')
        ans = float('inf')

        for n in nums:
            tmp = n
            while n % 2 == 0:
                n //= 2
            max_n = max(max_n, n)
            heapq.heappush(mh, (n, max(tmp, n*2)))
        
        while len(mh) == len(nums):
            elm, maxVal = heapq.heappop(mh)
            ans = min(ans, max_n - elm)
            if elm*2 <= maxVal:
                heapq.heappush(mh, (elm*2, maxVal))
                max_n = max(max_n, elm*2)
        
        return ans

'''


'''
1 2 3 4

ans = 
inf
3
2
1

SL: 
1 2 3 4
1 2 2 3
2 2 2 3

19
9
4
3

1 3 4 5 20
1 3 4 5 10
1 3 4 5 5
2 3 4 5 5
'''

'''
this fails some test cases
arr = SortedList(list(set(nums)))
        ans = float('inf')

        while True:
            smallest = arr[0]
            largest = arr[-1]
            deviation = largest - smallest

            if deviation >= ans:
                return ans
            ans = deviation

            if smallest % 2 == 1:
                arr.pop(0)
                arr.add(smallest*2)
            elif largest % 2 == 0:
                arr.pop(-1)
                arr.add(largest//2)
            else:
                break
        
        return ans
'''
        
25000