LeetCode 题解工作台

分糖果 II

排排坐,分糖果。 我们买了一些糖果 candies ,打算把它们分给排好队的 n = num_people 个小朋友。 给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类推,直到给最后一个小朋友 n 颗糖果。 然后,我们再回到队伍的起点,给第一个小朋友 n + 1 颗糖果,第二个小朋友 n +…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 数学·结合·模拟

bolt

答案摘要

我们可以直接模拟每一个人分到糖果的过程,按照题目描述的规则模拟即可。 时间复杂度 $O(\max(\sqrt{candies}, num\_people))$,空间复杂度 。其中 为糖果数量。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

排排坐,分糖果。

我们买了一些糖果 candies,打算把它们分给排好队的 n = num_people 个小朋友。

给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类推,直到给最后一个小朋友 n 颗糖果。

然后,我们再回到队伍的起点,给第一个小朋友 n + 1 颗糖果,第二个小朋友 n + 2 颗,依此类推,直到给最后一个小朋友 2 * n 颗糖果。

重复上述过程(每次都比上一次多给出一颗糖果,当到达队伍终点后再次从队伍起点开始),直到我们分完所有的糖果。注意,就算我们手中的剩下糖果数不够(不比前一次发出的糖果多),这些糖果也会全部发给当前的小朋友。

返回一个长度为 num_people、元素之和为 candies 的数组,以表示糖果的最终分发情况(即 ans[i] 表示第 i 个小朋友分到的糖果数)。

 

示例 1:

输入:candies = 7, num_people = 4
输出:[1,2,3,1]
解释:
第一次,ans[0] += 1,数组变为 [1,0,0,0]。
第二次,ans[1] += 2,数组变为 [1,2,0,0]。
第三次,ans[2] += 3,数组变为 [1,2,3,0]。
第四次,ans[3] += 1(因为此时只剩下 1 颗糖果),最终数组变为 [1,2,3,1]。

示例 2:

输入:candies = 10, num_people = 3
输出:[5,2,3]
解释:
第一次,ans[0] += 1,数组变为 [1,0,0]。
第二次,ans[1] += 2,数组变为 [1,2,0]。
第三次,ans[2] += 3,数组变为 [1,2,3]。
第四次,ans[0] += 4,最终数组变为 [5,2,3]。

 

提示:

  • 1 <= candies <= 10^9
  • 1 <= num_people <= 1000
lightbulb

解题思路

方法一:模拟

我们可以直接模拟每一个人分到糖果的过程,按照题目描述的规则模拟即可。

时间复杂度 O(max(candies,num_people))O(\max(\sqrt{candies}, num\_people)),空间复杂度 O(num_people)O(num\_people)。其中 candiescandies 为糖果数量。

1
2
3
4
5
6
7
8
9
10
class Solution:
    def distributeCandies(self, candies: int, num_people: int) -> List[int]:
        ans = [0] * num_people
        i = 0
        while candies:
            ans[i % num_people] += min(candies, i + 1)
            candies -= min(candies, i + 1)
            i += 1
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Candidate should focus on simulating the candy distribution step by step.

  • question_mark

    Watch for optimization efforts, especially when the candy count is very high.

  • question_mark

    Check if the candidate correctly handles cases with fewer candies than required for a full cycle.

warning

常见陷阱

外企场景
  • error

    Misunderstanding the cyclical distribution process and giving candies in the wrong order.

  • error

    Failing to properly handle the case where remaining candies are fewer than needed for a full cycle.

  • error

    Inefficient solutions that do not scale well with large inputs, like using brute force to simulate every candy distribution.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the distribution pattern, for example, skipping the increment for certain people.

  • arrow_right_alt

    Adjust the problem by giving candies to people based on a fixed pattern of skips.

  • arrow_right_alt

    Consider a different distribution order or a priority queue system for the people receiving candies.

help

常见问题

外企场景

分糖果 II题解:数学·结合·模拟 | LeetCode #1103 简单