LeetCode 1675 Minimize Deviation in Array (Python)

Posted by 小明MaxMing on February 24, 2023

题目

You are given an array nums of n positive integers.

You can perform two types of operations on any element of the array any number of times:

If the element is even, divide it by 2. For example, if the array is [1,2,3,4], then you can do this operation on the last element, and the array will be [1,2,3,2]. If the element is odd, multiply it by 2. For example, if the array is [1,2,3,4], then you can do this operation on the first element, and the array will be [2,2,3,4]. The deviation of the array is the maximum difference between any two elements in the array.

Return the minimum deviation the array can have after performing some number of operations.

解题思路

贪心,先把所有奇数 * 2,然后每次都取最大的数,和最小的数做差,更新结果,直到最大的数是奇数,结束循环

代码

class Solution:
    def minimumDeviation(self, nums: List[int]) -> int:
        q = []
        for n in nums:
            q.append(-2 * n if n & 1 else -n)
        res, maxv = float('inf'), max(q)
        heapq.heapify(q)
        while True:
            t = heapq.heappop(q)
            res = min(res, maxv - t)
            if t & 1:
                break
            t >>= 1
            maxv = max(maxv, t)
            heapq.heappush(q, t)
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅