LeetCode 题解工作台

优美的排列 II

给你两个整数 n 和 k ,请你构造一个答案列表 answer ,该列表应当包含从 1 到 n 的 n 个不同正整数,并同时满足下述条件: 假设该列表是 answer = [a 1 , a 2 , a 3 , ... , a n ] ,那么列表 [|a 1 - a 2 |, |a 2 - a 3 |…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·数学

bolt

答案摘要

先按照 `1, n, 2, n-1, 3,...` 构造答案数据 `ans` 的前 个数,共产生 个不同的整数。然后根据 的奇偶性确定从哪个数开始构造下一个数。 时间复杂度 ,忽略答案数组的空间消耗,空间复杂度 。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个整数 nk ,请你构造一个答案列表 answer ,该列表应当包含从 1nn 个不同正整数,并同时满足下述条件:

  • 假设该列表是 answer = [a1, a2, a3, ... , an] ,那么列表 [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] 中应该有且仅有 k 个不同整数。

返回列表 answer 。如果存在多种答案,只需返回其中 任意一种

 

示例 1:

输入:n = 3, k = 1
输出:[1, 2, 3]
解释:[1, 2, 3] 包含 3 个范围在 1-3 的不同整数,并且 [1, 1] 中有且仅有 1 个不同整数:1

示例 2:

输入:n = 3, k = 2
输出:[1, 3, 2]
解释:[1, 3, 2] 包含 3 个范围在 1-3 的不同整数,并且 [2, 1] 中有且仅有 2 个不同整数:1 和 2

 

提示:

  • 1 <= k < n <= 104

 

lightbulb

解题思路

方法一:构造

先按照 1, n, 2, n-1, 3,... 构造答案数据 ans 的前 kk 个数,共产生 k1k-1 个不同的整数。然后根据 kk 的奇偶性确定从哪个数开始构造下一个数。

时间复杂度 O(n)O(n),忽略答案数组的空间消耗,空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
    def constructArray(self, n: int, k: int) -> List[int]:
        l, r = 1, n
        ans = []
        for i in range(k):
            if i % 2 == 0:
                ans.append(l)
                l += 1
            else:
                ans.append(r)
                r -= 1
        for i in range(k, n):
            if k % 2 == 0:
                ans.append(r)
                r -= 1
            else:
                ans.append(l)
                l += 1
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    The candidate should demonstrate a solid understanding of how to manipulate arrays using mathematical methods.

  • question_mark

    Look for the candidate's ability to optimize both time and space complexity, focusing on an efficient O(n) solution.

  • question_mark

    Check if the candidate can identify and avoid common pitfalls in generating the array, such as unnecessary calculations.

warning

常见陷阱

外企场景
  • error

    Failing to account for distinct values: Ensure the arrangement does not have repeated numbers.

  • error

    Overcomplicating the solution: Aim for a simple yet efficient construction of the array without unnecessary operations.

  • error

    Incorrect handling of edge cases: Pay attention to how different values of k impact the arrangement and its validity.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    A variation of this problem could involve ensuring the arrangement meets specific conditions related to the positions of the integers.

  • arrow_right_alt

    A challenge could involve generating multiple valid arrangements that satisfy different values of k.

  • arrow_right_alt

    You might be tasked with modifying this problem to handle large values of n and k, pushing for further optimization.

help

常见问题

外企场景

优美的排列 II题解:数组·数学 | LeetCode #667 中等