
题目描述
你在一家生产小球的玩具厂工作,有 n
个小球,编号从 lowLimit
开始,到 highLimit
结束(包括 lowLimit
和 highLimit
,即 n == highLimit - lowLimit + 1
)。另有无限数量的盒子,编号从 1
到 infinity
。
你的工作是将每个小球放入盒子中,其中盒子的编号应当等于小球编号上每位数字的和。例如,编号 321
的小球应当放入编号 3 + 2 + 1 = 6
的盒子,而编号 10
的小球应当放入编号 1 + 0 = 1
的盒子。
给你两个整数 lowLimit
和 highLimit
,返回放有最多小球的盒子中的小球数量。如果有多个盒子都满足放有最多小球,只需返回其中任一盒子的小球数量。
示例 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
解法
方法一:数组 + 模拟
观察题目的数据范围,小球的编号最大不超过 \(10^5\),那么每个编号的各个位数之和的最大值小于 \(50\)。因此,我们可以直接开一个长度为 \(50\) 的数组 \(\textit{cnt}\) 来统计每个编号的各个位数之和的数量。
答案就是数组 \(\textit{cnt}\) 中的最大值。
时间复杂度 \(O(n \times \log_{10}m)\)。其中 \(n = \textit{highLimit} - \textit{lowLimit} + 1\),而 \(m = \textit{highLimit}\)。
| 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)
|
1
2
3
4
5
6
7
8
9
10
11
12
13 | class Solution {
public int countBalls(int lowLimit, int highLimit) {
int[] cnt = new int[50];
for (int i = lowLimit; i <= highLimit; ++i) {
int y = 0;
for (int x = i; x > 0; x /= 10) {
y += x % 10;
}
++cnt[y];
}
return Arrays.stream(cnt).max().getAsInt();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | class Solution {
public:
int countBalls(int lowLimit, int highLimit) {
int cnt[50] = {0};
int ans = 0;
for (int i = lowLimit; i <= highLimit; ++i) {
int y = 0;
for (int x = i; x; x /= 10) {
y += x % 10;
}
ans = max(ans, ++cnt[y]);
}
return ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | func countBalls(lowLimit int, highLimit int) (ans int) {
cnt := [50]int{}
for i := lowLimit; i <= highLimit; i++ {
y := 0
for x := i; x > 0; x /= 10 {
y += x % 10
}
cnt[y]++
if ans < cnt[y] {
ans = cnt[y]
}
}
return
}
|
| function countBalls(lowLimit: number, highLimit: number): number {
const cnt: number[] = Array(50).fill(0);
for (let i = lowLimit; i <= highLimit; ++i) {
let y = 0;
for (let x = i; x; x = Math.floor(x / 10)) {
y += x % 10;
}
++cnt[y];
}
return Math.max(...cnt);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | impl Solution {
pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 {
let mut cnt = vec![0; 50];
for x in low_limit..=high_limit {
let mut y = 0;
let mut n = x;
while n > 0 {
y += n % 10;
n /= 10;
}
cnt[y as usize] += 1;
}
*cnt.iter().max().unwrap()
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | /**
* @param {number} lowLimit
* @param {number} highLimit
* @return {number}
*/
var countBalls = function (lowLimit, highLimit) {
const cnt = Array(50).fill(0);
for (let i = lowLimit; i <= highLimit; ++i) {
let y = 0;
for (let x = i; x; x = Math.floor(x / 10)) {
y += x % 10;
}
++cnt[y];
}
return Math.max(...cnt);
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | public class Solution {
public int CountBalls(int lowLimit, int highLimit) {
int[] cnt = new int[50];
for (int x = lowLimit; x <= highLimit; x++) {
int y = 0;
int n = x;
while (n > 0) {
y += n % 10;
n /= 10;
}
cnt[y]++;
}
return cnt.Max();
}
}
|