LeetCode 题解工作台

数组的最小偏移量

给你一个由 n 个正整数组成的数组 nums 。 你可以对数组的任意元素执行任意次数的两类操作: 如果元素是 偶数 , 除以 2 例如,如果数组是 [1,2,3,4] ,那么你可以对最后一个元素执行此操作,使其变成 [1,2,3, 2 ] 如果元素是 奇数 , 乘上 2 例如,如果数组是 [1,2,…

category

4

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

困难 · 贪心·invariant

bolt

答案摘要

直观上,为了得到数组的最小偏移量,我们需要将减小数组的最大值,增大数组的最小值。 由于每次可以执行乘、除两种操作:将奇数乘以 ;将偶数除以 ,情况较为复杂,我们可以将奇数统一乘以 ,转成偶数,这样就等价于只有一种除法操作。除法操作只能减少某个数,而只有减少最大值,结果才可能更优。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个由 n 个正整数组成的数组 nums

你可以对数组的任意元素执行任意次数的两类操作:

  • 如果元素是 偶数除以 2
    • 例如,如果数组是 [1,2,3,4] ,那么你可以对最后一个元素执行此操作,使其变成 [1,2,3,2]
  • 如果元素是 奇数乘上 2
    • 例如,如果数组是 [1,2,3,4] ,那么你可以对第一个元素执行此操作,使其变成 [2,2,3,4]

数组的 偏移量 是数组中任意两个元素之间的 最大差值

返回数组在执行某些操作之后可以拥有的 最小偏移量

 

示例 1:

输入:nums = [1,2,3,4]
输出:1
解释:你可以将数组转换为 [1,2,3,2],然后转换成 [2,2,3,2],偏移量是 3 - 2 = 1

示例 2:

输入:nums = [4,1,5,20,3]
输出:3
解释:两次操作后,你可以将数组转换为 [4,2,5,5,3],偏移量是 5 - 2 = 3

示例 3:

输入:nums = [2,10,8]
输出:3

 

提示:

  • n == nums.length
  • 2 <= n <= 5 * 104
  • 1 <= nums[i] <= 109
lightbulb

解题思路

方法一:贪心 + 优先队列

直观上,为了得到数组的最小偏移量,我们需要将减小数组的最大值,增大数组的最小值。

由于每次可以执行乘、除两种操作:将奇数乘以 22;将偶数除以 22,情况较为复杂,我们可以将奇数统一乘以 22,转成偶数,这样就等价于只有一种除法操作。除法操作只能减少某个数,而只有减少最大值,结果才可能更优。

因此,我们用优先队列(大根堆)维护数组的最大值,每次取出堆顶元素做除法操作,将新值放入堆中,并且更新最小值以及堆顶元素与最小值的差值的最小值。

当堆顶元素为奇数时,操作停止。

时间复杂度 O(nlogn×logm)O(n\log n \times \log m)。其中 nn, mm 分别是数组 nums 的长度以及数组的最大元素。由于数组中的最大元素除以 22 的操作最多有 O(logm)O(\log m) 次,因此全部元素除以 22 的操作最多有 O(nlogm)O(n\log m) 次。每次弹出、放入堆的操作,时间复杂度为 O(logn)O(\log n)。因此,总的时间复杂度为 O(nlogn×logm)O(n\log n \times \log m)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)
        ans = -h[0] - mi
        while h[0] % 2 == 0:
            x = heappop(h) // 2
            heappush(h, x)
            mi = min(mi, -x)
            ans = min(ans, -h[0] - mi)
        return ans
speed

复杂度分析

指标
时间complexity is O(n log n) due to heap operations performed for each element at most log(max(nums)) times. Space complexity is O(n) for storing the heap and tracking the minimum element.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidates who attempt naive sorting without heap will likely hit timeouts.

  • question_mark

    Look for recognition that the greedy approach must focus on the maximum element while preserving array invariants.

  • question_mark

    Ensure they understand why odd numbers are only multiplied once and even numbers can be repeatedly halved.

warning

常见陷阱

外企场景
  • error

    Failing to normalize odd numbers initially, causing incorrect deviation calculation.

  • error

    Using a min-heap instead of a max-heap, which reverses the greedy strategy.

  • error

    Not updating the current minimum when halving the maximum, leading to wrong deviation results.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Minimize deviation with additional constraint on number of operations allowed.

  • arrow_right_alt

    Find maximum deviation instead of minimum after allowed operations.

  • arrow_right_alt

    Solve for arrays containing zeros and ones with same doubling/halving rules.

help

常见问题

外企场景

数组的最小偏移量题解:贪心·invariant | LeetCode #1675 困难