LeetCode 题解工作台

盒子中小球的最大数量

你在一家生产小球的玩具厂工作,有 n 个小球,编号从 lowLimit 开始,到 highLimit 结束(包括 lowLimit 和 highLimit ,即 n == highLimit - lowLimit + 1 )。另有无限数量的盒子,编号从 1 到 infinity 。 你的工作是将每个…

category

3

题型

code_blocks

8

代码语言

hub

3

相关题

当前训练重点

简单 · 哈希·数学

bolt

答案摘要

观察题目的数据范围,小球的编号最大不超过 ,那么每个编号的各个位数之和的最大值小于 。因此,我们可以直接开一个长度为 的数组 来统计每个编号的各个位数之和的数量。 答案就是数组 中的最大值。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

你在一家生产小球的玩具厂工作,有 n 个小球,编号从 lowLimit 开始,到 highLimit 结束(包括 lowLimit 和 highLimit ,即 n == highLimit - lowLimit + 1)。另有无限数量的盒子,编号从 1infinity

你的工作是将每个小球放入盒子中,其中盒子的编号应当等于小球编号上每位数字的和。例如,编号 321 的小球应当放入编号 3 + 2 + 1 = 6 的盒子,而编号 10 的小球应当放入编号 1 + 0 = 1 的盒子。

给你两个整数 lowLimithighLimit ,返回放有最多小球的盒子中的小球数量如果有多个盒子都满足放有最多小球,只需返回其中任一盒子的小球数量。

 

示例 1:

输入:lowLimit = 1, highLimit = 10
输出:2
解释:
盒子编号:1 2 3 4 5 6 7 8 9 10 11 ...
小球数量:2 1 1 1 1 1 1 1 1 0  0  ...
编号 1 的盒子放有最多小球,小球数量为 2 。

示例 2:

输入:lowLimit = 5, highLimit = 15
输出:2
解释:
盒子编号:1 2 3 4 5 6 7 8 9 10 11 ...
小球数量:1 1 1 1 2 2 1 1 1 0  0  ...
编号 5 和 6 的盒子放有最多小球,每个盒子中的小球数量都是 2 。

示例 3:

输入:lowLimit = 19, highLimit = 28
输出:2
解释:
盒子编号:1 2 3 4 5 6 7 8 9 10 11 12 ...
小球数量:0 1 1 1 1 1 1 1 1 2  0  0  ...
编号 10 的盒子放有最多小球,小球数量为 2 。

 

提示:

  • 1 <= lowLimit <= highLimit <= 105
lightbulb

解题思路

方法一:数组 + 模拟

观察题目的数据范围,小球的编号最大不超过 10510^5,那么每个编号的各个位数之和的最大值小于 5050。因此,我们可以直接开一个长度为 5050 的数组 cnt\textit{cnt} 来统计每个编号的各个位数之和的数量。

答案就是数组 cnt\textit{cnt} 中的最大值。

时间复杂度 O(n×log10m)O(n \times \log_{10}m)。其中 n=highLimitlowLimit+1n = \textit{highLimit} - \textit{lowLimit} + 1,而 m=highLimitm = \textit{highLimit}

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def countBalls(self, lowLimit: int, highLimit: int) -> int:
        cnt = [0] * 50
        for x in range(lowLimit, highLimit + 1):
            y = 0
            while x:
                y += x % 10
                x //= 10
            cnt[y] += 1
        return max(cnt)
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate can identify the importance of using a hash table to store the count of balls in each box.

  • question_mark

    Ensure the candidate understands the sum of digits concept and its relevance to the problem.

  • question_mark

    Assess the candidate's ability to optimize the solution given the constraints, especially time complexity.

warning

常见陷阱

外企场景
  • error

    Failing to efficiently compute the sum of digits for each number.

  • error

    Not handling the edge cases, such as the smallest range (lowLimit = highLimit).

  • error

    Using inefficient data structures that could increase time complexity unnecessarily.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the problem to consider larger ranges, forcing candidates to optimize the approach further.

  • arrow_right_alt

    Introduce additional constraints on the ball or box numbers, requiring adjustments to the sum of digits logic.

  • arrow_right_alt

    Allow multiple queries for different ranges and require a solution that can handle batch processing.

help

常见问题

外企场景

盒子中小球的最大数量题解:哈希·数学 | LeetCode #1742 简单