LeetCode 题解工作台

漂亮数组

如果长度为 n 的数组 nums 满足下述条件,则认为该数组是一个 漂亮数组 : nums 是由范围 [1, n] 的整数组成的一个排列。 对于每个 0 ,均不存在下标 k ( i )使得 2 * nums[k] == nums[i] + nums[j] 。 给你整数 n ,返回长度为 n 的任一 …

category

3

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·数学

bolt

答案摘要

根据题意,漂亮数组 需要满足对于任意 , $A_k*2 \neq A_i+A_j$。 我们可以发现,不等式左侧一定是偶数,那么我们只要保证不等式右侧 和 分别是一奇一偶,那么不等式就恒成立。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·数学 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

如果长度为 n 的数组 nums 满足下述条件,则认为该数组是一个 漂亮数组

  • nums 是由范围 [1, n] 的整数组成的一个排列。
  • 对于每个 0 <= i < j < n ,均不存在下标 ki < k < j)使得 2 * nums[k] == nums[i] + nums[j]

给你整数 n ,返回长度为 n 的任一 漂亮数组 。本题保证对于给定的 n 至少存在一个有效答案。

 

示例 1 :

输入:n = 4
输出:[2,1,4,3]

示例 2 :

输入:n = 5
输出:[3,1,2,5,4]

 

提示:

  • 1 <= n <= 1000

 

lightbulb

解题思路

方法一:分治

根据题意,漂亮数组 AA 需要满足对于任意 i<k<ji<k<j, Ak2Ai+AjA_k*2 \neq A_i+A_j

我们可以发现,不等式左侧一定是偶数,那么我们只要保证不等式右侧 AiA_iAjA_j 分别是一奇一偶,那么不等式就恒成立。

利用分治,我们将 nn 缩小规模为原来的一半,递归调用,可以得到两个漂亮数组 leftleft, rightright。我们将 leftleft 中每个元素 xix_i 变为 xi21x_i*2-1 可以得到一个奇数数组;将 rightright 中每个元素 xix_i 变为 xi2x_i*2,可以得到一个偶数数组。这两个数组仍然是漂亮数组。

基于一个性质,将漂亮数组中的每个元素 xix_i 变换为 kxi+bkx_i+b,得到的数组仍然是漂亮数组。

将这两个漂亮数组合并在一起,由于满足一奇一偶,那么合并后的数组也是漂亮数组,从而得到了答案。

时间复杂度 O(nlogn)O(nlogn)

1
2
3
4
5
6
7
8
9
10
class Solution:
    def beautifulArray(self, n: int) -> List[int]:
        if n == 1:
            return [1]
        left = self.beautifulArray((n + 1) >> 1)
        right = self.beautifulArray(n >> 1)
        left = [x * 2 - 1 for x in left]
        right = [x * 2 for x in right]
        return left + right
speed

复杂度分析

指标
时间O(N \log N)
空间O(N \log N)
psychology

面试官常问的追问

外企场景
  • question_mark

    They want you to stop thinking in terms of local swaps and notice a global construction based on parity.

  • question_mark

    A strong answer explains why 2x-1 and 2x preserve beauty instead of only presenting code that seems to work.

  • question_mark

    If they ask for n = 4 or n = 5 by hand, they are checking whether you understand the recursive build, not memorized output.

warning

常见陷阱

外企场景
  • error

    Trying to greedily place the next number often creates a later average collision that is hard to repair.

  • error

    Assuming you must return the sample order is wrong; Beautiful Array accepts any valid permutation.

  • error

    Forgetting to discard mapped values greater than n will break the permutation and produce duplicates or out-of-range numbers.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Return any beautiful ordering for only a subset of numbers, which keeps the same odd-even recursive idea but changes filtering.

  • arrow_right_alt

    Count how many forbidden average triples appear in a given permutation, which flips the problem from constructive math to verification.

  • arrow_right_alt

    Build the array iteratively with memoization versus top-down recursion, which tests whether you can preserve the same parity argument in a different implementation style.

help

常见问题

外企场景

漂亮数组题解:数组·数学 | LeetCode #932 中等