LeetCode 题解工作台

构造元素不等于两相邻元素平均值的数组

给你一个 下标从 0 开始 的数组 nums ,数组由若干 互不相同的 整数组成。你打算重新排列数组中的元素以满足:重排后,数组中的每个元素都 不等于 其两侧相邻元素的 平均值 。 更公式化的说法是,重新排列的数组应当满足这一属性:对于范围 1 中的每个 i , (nums[i-1] + nums[…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 贪心·invariant

bolt

答案摘要

由于数组中的元素是互不相同的,我们可以先对数组进行排序,然后将数组分成两部分,将前一半的元素放到答案数组中的偶数位置,将后一半的元素放到答案数组中的奇数位置。这样,对于每个元素,它的两个相邻元素都不会等于它的平均值。 时间复杂度 $O(n \times \log n)$,空间复杂度 。其中 是数组 的长度。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个 下标从 0 开始 的数组 nums ,数组由若干 互不相同的 整数组成。你打算重新排列数组中的元素以满足:重排后,数组中的每个元素都 不等于 其两侧相邻元素的 平均值

更公式化的说法是,重新排列的数组应当满足这一属性:对于范围 1 <= i < nums.length - 1 中的每个 i(nums[i-1] + nums[i+1]) / 2 不等于 nums[i] 均成立 。

返回满足题意的任一重排结果。

 

示例 1:

输入:nums = [1,2,3,4,5]
输出:[1,2,4,5,3]
解释:
i=1, nums[i] = 2, 两相邻元素平均值为 (1+4) / 2 = 2.5
i=2, nums[i] = 4, 两相邻元素平均值为 (2+5) / 2 = 3.5
i=3, nums[i] = 5, 两相邻元素平均值为 (4+3) / 2 = 3.5

示例 2:

输入:nums = [6,2,0,9,7]
输出:[9,7,6,2,0]
解释:
i=1, nums[i] = 7, 两相邻元素平均值为 (9+6) / 2 = 7.5
i=2, nums[i] = 6, 两相邻元素平均值为 (7+2) / 2 = 4.5
i=3, nums[i] = 2, 两相邻元素平均值为 (6+0) / 2 = 3

 

提示:

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

解题思路

方法一:排序

由于数组中的元素是互不相同的,我们可以先对数组进行排序,然后将数组分成两部分,将前一半的元素放到答案数组中的偶数位置,将后一半的元素放到答案数组中的奇数位置。这样,对于每个元素,它的两个相邻元素都不会等于它的平均值。

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

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def rearrangeArray(self, nums: List[int]) -> List[int]:
        nums.sort()
        n = len(nums)
        m = (n + 1) // 2
        ans = []
        for i in range(m):
            ans.append(nums[i])
            if i + m < n:
                ans.append(nums[i + m])
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidates should focus on the greedy nature of the solution, ensuring no element becomes the average of its neighbors.

  • question_mark

    Look for an understanding of invariants and how the rearranged array maintains these properties after sorting.

  • question_mark

    Evaluate the candidate's ability to handle edge cases where the smallest and largest values are at the boundaries of the array.

warning

常见陷阱

外企场景
  • error

    Not considering the possibility of adjacent elements being too close, leading to violations of the average condition.

  • error

    Failing to rearrange the array optimally, resulting in a non-working solution that does not break the neighbor average condition.

  • error

    Overcomplicating the problem with unnecessary checks or operations that increase the time complexity.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Try the problem with arrays of varying lengths, from the minimum size to the maximum allowed by the constraints.

  • arrow_right_alt

    Test with arrays where values are arranged in increasing or decreasing order, and analyze the effect on the solution's correctness.

  • arrow_right_alt

    Introduce duplicate elements into the problem and adjust the constraints to see if the greedy approach can still be applied.

help

常见问题

外企场景

构造元素不等于两相邻元素平均值的数组题解:贪心·invariant | LeetCode #1968 中等