LeetCode 题解工作台

修改两个元素的最小分数

给你一个下标从 0 开始的整数数组 nums 。 nums 的 最小 得分是满足 0 的 |nums[i] - nums[j]| 的最小值。 nums 的 最大 得分是满足 0 的 |nums[i] - nums[j]| 的最大值。 nums 的分数是 最大 得分与 最小 得分的和。 我们的目标是最…

category

3

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

中等 · 贪心·invariant

bolt

答案摘要

根据题意我们知道,最小得分实际上是排序数组相邻两个元素的最小差值,最大得分是排序数组首尾元素的差值。数组 的分数是最小得分与最大得分的和。 因此,我们可以先对数组进行排序。由于题目允许我们修改数组中最多两个元素的值,我们可以通过修改一个数,让其跟数组中的另一个数相同,使得最小得分为 ,那么数组 的分数实际上就是最大得分。我们可以选择进行如下修改之一:

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的整数数组 nums 。

  • nums最小 得分是满足 0 <= i < j < nums.length 的 |nums[i] - nums[j]| 的最小值。
  • nums最大 得分是满足 0 <= i < j < nums.length 的 |nums[i] - nums[j]| 的最大值。
  • nums 的分数是 最大 得分与 最小 得分的和。

我们的目标是最小化 nums 的分数。你 最多 可以修改 nums 中 2 个元素的值。

请你返回修改 nums 中 至多两个 元素的值后,可以得到的 最小分数 。

|x| 表示 x 的绝对值。

 

示例 1:

输入:nums = [1,4,3]
输出:0
解释:将 nums[1] 和 nums[2] 的值改为 1 ,nums 变为 [1,1,1] 。|nums[i] - nums[j]| 的值永远为 0 ,所以我们返回 0 + 0 = 0 。

示例 2:

输入:nums = [1,4,7,8,5]
输出:3
解释:
将 nums[0] 和 nums[1] 的值变为 6 ,nums 变为 [6,6,7,8,5] 。
最小得分是 i = 0 且 j = 1 时得到的 |nums[i] - nums[j]| = |6 - 6| = 0 。
最大得分是 i = 3 且 j = 4 时得到的 |nums[i] - nums[j]| = |8 - 5| = 3 。
最大得分与最小得分之和为 3 。这是最优答案。

 

提示:

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

解题思路

方法一:排序 + 贪心

根据题意我们知道,最小得分实际上是排序数组相邻两个元素的最小差值,最大得分是排序数组首尾元素的差值。数组 numsnums 的分数是最小得分与最大得分的和。

因此,我们可以先对数组进行排序。由于题目允许我们修改数组中最多两个元素的值,我们可以通过修改一个数,让其跟数组中的另一个数相同,使得最小得分为 00,那么数组 numsnums 的分数实际上就是最大得分。我们可以选择进行如下修改之一:

  1. 修改最小的两个数为 nums[2]nums[2],那么最大得分为 nums[n1]nums[2]nums[n - 1] - nums[2]
  2. 修改最小的一个数为 nums[1]nums[1],最大的一个数为 nums[n2]nums[n - 2],那么最大得分为 nums[n2]nums[1]nums[n - 2] - nums[1]
  3. 修改最大的两个数为 nums[n3]nums[n - 3],那么最大得分为 nums[n3]nums[0]nums[n - 3] - nums[0]

最后,我们返回上述三种修改的得分的最小值即可。

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

相似题目:

1
2
3
4
5
class Solution:
    def minimizeSum(self, nums: List[int]) -> int:
        nums.sort()
        return min(nums[-1] - nums[2], nums[-2] - nums[1], nums[-3] - nums[0])
speed

复杂度分析

指标
时间complexity is dependent on the sorting step, which is O(n log n), and space complexity is O(n) for the array processing. The greedy approach and invariant validation add minimal overhead.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate focuses on adjusting the extremes of the array for minimization.

  • question_mark

    The candidate can correctly explain the impact of sorting and greedy choices.

  • question_mark

    The candidate handles edge cases effectively and ensures correctness through validation.

warning

常见陷阱

外企场景
  • error

    Ignoring the importance of modifying the extremes (max and min) first.

  • error

    Not validating the final array after changes, potentially leading to incorrect results.

  • error

    Failing to account for edge cases such as very small arrays or large values.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Changing more than two elements and observing the impact on score.

  • arrow_right_alt

    Handling arrays with only one possible change, where the result is trivial.

  • arrow_right_alt

    Optimizing for specific constraints such as arrays with repeated elements.

help

常见问题

外企场景

修改两个元素的最小分数题解:贪心·invariant | LeetCode #2567 中等