LeetCode 题解工作台

三次操作后最大值与最小值的最小差

给你一个数组 nums 。 每次操作你可以选择 nums 中的任意一个元素并将它改成 任意值 。 在 执行最多三次移动后 ,返回 nums 中最大值与最小值的最小差值。 示例 1: 输入: nums = [5,3,2,4] 输出: 0 解释: 我们最多可以走 3 步。 第一步,将 2 变为 3 。 …

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 贪心·invariant

bolt

答案摘要

我们可以先判断数组长度是否小于 ,如果小于 ,那么直接返回 。 否则,我们将数组排序,然后贪心地选择数组左边最小的 个数和右边最小的 $r = 3 - l$ 个数,取其中最小的差值 $nums[n - 1 - r] - nums[l]$ 即可。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个数组 nums 。

每次操作你可以选择 nums 中的任意一个元素并将它改成 任意值

在 执行最多三次移动后 ,返回 nums 中最大值与最小值的最小差值。

 

示例 1:

输入:nums = [5,3,2,4]
输出:0
解释:我们最多可以走 3 步。
第一步,将 2 变为 3 。 nums 变成 [5,3,3,4] 。
第二步,将 4 改为 3 。 nums 变成 [5,3,3,3] 。
第三步,将 5 改为 3 。 nums 变成 [3,3,3,3] 。
执行 3 次移动后,最小值和最大值之间的差值为 3 - 3 = 0 。

示例 2:

输入:nums = [1,5,0,10,14]
输出:1
解释:我们最多可以走 3 步。
第一步,将 5 改为 0 。 nums变成 [1,0,0,10,14] 。
第二步,将 10 改为 0 。 nums变成 [1,0,0,0,14] 。
第三步,将 14 改为 1 。 nums变成 [1,0,0,0,1] 。
执行 3 步后,最小值和最大值之间的差值为 1 - 0 = 1 。
可以看出,没有办法可以在 3 步内使差值变为0。

示例 3:

输入:nums = [3,100,20]
输出:0
解释:我们最多可以走 3 步。
第一步,将 100 改为 7 。 nums 变成 [3,7,20] 。
第二步,将 20 改为 7 。 nums 变成 [3,7,7] 。
第三步,将 3 改为 7 。 nums 变成 [7,7,7] 。
执行 3 步后,最小值和最大值之间的差值是 7 - 7 = 0。

 

提示:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109
lightbulb

解题思路

方法一:排序 + 贪心

我们可以先判断数组长度是否小于 55,如果小于 55,那么直接返回 00

否则,我们将数组排序,然后贪心地选择数组左边最小的 l=[0,..3]l=[0,..3] 个数和右边最小的 r=3lr = 3 - l 个数,取其中最小的差值 nums[n1r]nums[l]nums[n - 1 - r] - nums[l] 即可。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(logn)O(\log n)。其中 nn 为数组 nums 的长度。

相似题目:

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def minDifference(self, nums: List[int]) -> int:
        n = len(nums)
        if n < 5:
            return 0
        nums.sort()
        ans = inf
        for l in range(4):
            r = 3 - l
            ans = min(ans, nums[n - 1 - r] - nums[l])
        return ans
speed

复杂度分析

指标
时间O(n)
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for a candidate who can efficiently implement the greedy approach.

  • question_mark

    The candidate should demonstrate an understanding of the impact of sorting on time complexity.

  • question_mark

    A good candidate will consider edge cases where fewer than four elements are present in the array.

warning

常见陷阱

外企场景
  • error

    Failing to recognize that only three moves are allowed, and thus choosing more elements than necessary.

  • error

    Not considering edge cases where the array is too small to perform any moves.

  • error

    Overlooking the importance of sorting the array for efficient calculation of possible differences.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allowing more than three moves and testing the impact on time complexity.

  • arrow_right_alt

    Implementing the solution without sorting, using a more efficient selection method.

  • arrow_right_alt

    Using a dynamic programming approach to test all combinations of three changes.

help

常见问题

外企场景

三次操作后最大值与最小值的最小差题解:贪心·invariant | LeetCode #1509 中等