LeetCode 题解工作台

拆分成最多数目的正偶数之和

给你一个整数 finalSum 。请你将它拆分成若干个 互不相同 的正偶数之和,且拆分出来的正偶数数目 最多 。 比方说,给你 finalSum = 12 ,那么这些拆分是 符合要求 的(互不相同的正偶数且和为 finalSum ): (2 + 10) , (2 + 4 + 6) 和 (4 + 8)…

category

3

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

中等 · 回溯·pruning

bolt

答案摘要

如果 是奇数,那么无法拆分成若干个互不相同的正偶数之和,直接返回空数组。 否则,我们可以贪心地按照 $2, 4, 6, \cdots$ 的顺序拆分 ,直到 无法再拆分出一个不同的正偶数为止,此时我们将剩余的 加到最后一个正偶数上即可。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 回溯·pruning 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数 finalSum 。请你将它拆分成若干个 互不相同 的正偶数之和,且拆分出来的正偶数数目 最多 。

  • 比方说,给你 finalSum = 12 ,那么这些拆分是 符合要求 的(互不相同的正偶数且和为 finalSum):(2 + 10) ,(2 + 4 + 6) 和 (4 + 8) 。它们中,(2 + 4 + 6) 包含最多数目的整数。注意 finalSum 不能拆分成 (2 + 2 + 4 + 4) ,因为拆分出来的整数必须互不相同。

请你返回一个整数数组,表示将整数拆分成 最多 数目的正偶数数组。如果没有办法将 finalSum 进行拆分,请你返回一个  数组。你可以按 任意 顺序返回这些整数。

 

示例 1:

输入:finalSum = 12
输出:[2,4,6]
解释:以下是一些符合要求的拆分:(2 + 10)(2 + 4 + 6) (4 + 8) 。
(2 + 4 + 6) 为最多数目的整数,数目为 3 ,所以我们返回 [2,4,6] 。
[2,6,4] ,[6,2,4] 等等也都是可行的解。

示例 2:

输入:finalSum = 7
输出:[]
解释:没有办法将 finalSum 进行拆分。
所以返回空数组。

示例 3:

输入:finalSum = 28
输出:[6,8,2,12]
解释:以下是一些符合要求的拆分:(2 + 26)(6 + 8 + 2 + 12)(4 + 24) 。
(6 + 8 + 2 + 12) 有最多数目的整数,数目为 4 ,所以我们返回 [6,8,2,12] 。
[10,2,4,12] ,[6,2,4,16] 等等也都是可行的解。

 

提示:

  • 1 <= finalSum <= 1010
lightbulb

解题思路

方法一:贪心

如果 finalSum\textit{finalSum} 是奇数,那么无法拆分成若干个互不相同的正偶数之和,直接返回空数组。

否则,我们可以贪心地按照 2,4,6,2, 4, 6, \cdots 的顺序拆分 finalSum\textit{finalSum},直到 finalSum\textit{finalSum} 无法再拆分出一个不同的正偶数为止,此时我们将剩余的 finalSum\textit{finalSum} 加到最后一个正偶数上即可。

时间复杂度 O(finalSum)O(\sqrt{\textit{finalSum}}),忽略答案数组的空间消耗,空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def maximumEvenSplit(self, finalSum: int) -> List[int]:
        if finalSum & 1:
            return []
        ans = []
        i = 2
        while i <= finalSum:
            finalSum -= i
            ans.append(i)
            i += 2
        ans[-1] += finalSum
        return ans
speed

复杂度分析

指标
时间complexity varies with finalSum and depends on the number of unique even integers that can be included; greedy selection with pruning minimizes unnecessary iterations. Space complexity depends on the size of the split array and the recursion depth if backtracking is used.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Ask about handling odd finalSum edge cases.

  • question_mark

    Probe understanding of greedy selection vs. full backtracking.

  • question_mark

    Check awareness of pruning to avoid unnecessary recursion.

warning

常见陷阱

外企场景
  • error

    Forgetting to check if finalSum is odd before starting.

  • error

    Not enforcing uniqueness of even integers in the split.

  • error

    Overcomplicating the solution without leveraging greedy incremental selection.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allowing repeated even integers and maximizing count.

  • arrow_right_alt

    Minimizing the largest integer in the split instead of maximizing count.

  • arrow_right_alt

    Splitting into positive odd integers instead of even integers.

help

常见问题

外企场景

拆分成最多数目的正偶数之和题解:回溯·pruning | LeetCode #2178 中等