LeetCode 题解工作台

给小朋友们分糖果 II

给你两个正整数 n 和 limit 。 请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。 示例 1: 输入: n = 5, limit = 2 输出: 3 解释: 总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数学·结合·combinatorics

bolt

答案摘要

根据题目描述,我们需要将 个糖果分给 个小孩,每个小孩分到的糖果数在 $[0, limit]$ 之间。 这实际上等价于把 个球放入 个盒子中。由于盒子可以为空,我们可以再增加 个虚拟球,然后再利用隔板法,即一共有 $n + 3$ 个球,我们在其中 $n + 3 - 1$ 个位置插入 个隔板,从而将实际的 个球分成 组,并且允许盒子为空,因此初始方案数为 $C_{n + 2}^2$。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个正整数 n 和 limit 。

请你将 n 颗糖果分给 3 位小朋友,确保没有任何小朋友得到超过 limit 颗糖果,请你返回满足此条件下的 总方案数 。

 

示例 1:

输入:n = 5, limit = 2
输出:3
解释:总共有 3 种方法分配 5 颗糖果,且每位小朋友的糖果数不超过 2 :(1, 2, 2) ,(2, 1, 2) 和 (2, 2, 1) 。

示例 2:

输入:n = 3, limit = 3
输出:10
解释:总共有 10 种方法分配 3 颗糖果,且每位小朋友的糖果数不超过 3 :(0, 0, 3) ,(0, 1, 2) ,(0, 2, 1) ,(0, 3, 0) ,(1, 0, 2) ,(1, 1, 1) ,(1, 2, 0) ,(2, 0, 1) ,(2, 1, 0) 和 (3, 0, 0) 。

 

提示:

  • 1 <= n <= 106
  • 1 <= limit <= 106
lightbulb

解题思路

方法一:组合数学 + 容斥原理

根据题目描述,我们需要将 nn 个糖果分给 33 个小孩,每个小孩分到的糖果数在 [0,limit][0, limit] 之间。

这实际上等价于把 nn 个球放入 33 个盒子中。由于盒子可以为空,我们可以再增加 33 个虚拟球,然后再利用隔板法,即一共有 n+3n + 3 个球,我们在其中 n+31n + 3 - 1 个位置插入 22 个隔板,从而将实际的 nn 个球分成 33 组,并且允许盒子为空,因此初始方案数为 Cn+22C_{n + 2}^2

我们需要在这些方案中,排除掉存在盒子分到的小球数超过 limitlimit 的方案。考虑其中有一个盒子分到的小球数超过 limitlimit,那么剩下的球(包括虚拟球)最多有 n+3(limit+1)=nlimit+2n + 3 - (limit + 1) = n - limit + 2 个,位置数为 nlimit+1n - limit + 1,因此方案数为 Cnlimit+12C_{n - limit + 1}^2。由于存在 33 个盒子,因此这样的方案数为 3×Cnlimit+123 \times C_{n - limit + 1}^2。这样子算,我们会多排除掉同时存在两个盒子分到的小球数超过 limitlimit 的方案,因此我们需要再加上这样的方案数,即 3×Cn2×limit23 \times C_{n - 2 \times limit}^2

时间复杂度 O(1)O(1),空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def distributeCandies(self, n: int, limit: int) -> int:
        if n > 3 * limit:
            return 0
        ans = comb(n + 2, 2)
        if n > limit:
            ans -= 3 * comb(n - limit + 1, 2)
        if n - 2 >= 2 * limit:
            ans += 3 * comb(n - 2 * limit, 2)
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Strong understanding of combinatorics and enumeration techniques.

  • question_mark

    Ability to optimize a brute-force solution for large input sizes.

  • question_mark

    Familiarity with problem-solving patterns involving distribution under constraints.

warning

常见陷阱

外企场景
  • error

    Forgetting to account for the limit on the number of candies each child can receive.

  • error

    Incorrectly assuming the problem can be solved through brute-force enumeration without optimization.

  • error

    Misunderstanding the problem's constraints, leading to an inefficient or incorrect solution.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Distribute candies among a different number of children.

  • arrow_right_alt

    Distribute candies with varying limits per child.

  • arrow_right_alt

    Find the number of ways to distribute candies when each child must receive at least one candy.

help

常见问题

外企场景

给小朋友们分糖果 II题解:数学·结合·combinatorics | LeetCode #2929 中等