1675. Minimize Deviation in Array

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1675

class Solution:
    def minimumDeviation(self, nums: List[int]) -> int:
        h = []
        mi = inf
        for v in nums:
            if v & 1:
                v <<= 1
            h.append(-v)
            mi = min(mi, v)
        heapify(h)
        res = -h[0] - mi
        while h[0] % 2 == 0:
            x = heappop(h) // 2
            heappush(h, x)
            mi = min(mi, -x)
            res = min(res, -h[0] - mi)
        return res