当前位置:网站首页>【每日3题(3)】盒子中小球的最大数量
【每日3题(3)】盒子中小球的最大数量
2022-06-27 13:35:00 【程序猿不脱发2】
题目:
你在一家生产小球的玩具厂工作,有 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 <= 10^5
思路:
重点在求位数和,取余10, 除以10;
java代码:
class Solution {
public int countBalls(int lowLimit, int highLimit) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = lowLimit; i <= highLimit; i++) {
int sum = sum(i);
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
int max =0;
for(Map.Entry<Integer, Integer> entry:map.entrySet()) {
max = Math.max(entry.getValue(), max);
}
return max;
}
private int sum(int i) {
int sum = 0;
while (i != 0) {
sum += i % 10;
i /= 10;
}
return sum;
}
}
边栏推荐
- CMOS级电路分析
- IJCAI 2022 | greatly improve the effect of zero sample learning method with one line of code. Nanjing Institute of Technology & Oxford proposed the plug and play classifier module
- MySQL locking mechanism and four isolation levels
- Good luck today
- What kind of air conditioner is this?
- Differences in perspectives of thinking
- Shell 简明教程
- 【第27天】给定一个整数 n ,打印出1到n的全排列 | 全排列模板
- NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
- Can flush open an account for stock trading? Is it safe?
猜你喜欢

Pytorch learning 1 (learning documents on the official website)

Journal quotidien des questions (6)
![[dynamic programming] - Knapsack Problem](/img/27/c48284f15e3f80305d7ce7c02d4378.png)
[dynamic programming] - Knapsack Problem

Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices

全球芯片市场或陷入停滞,中国芯片逆势扩张加速提升自给率

万物互联时代到来,锐捷发布场景化无线零漫游方案

Axi bus

Implementation of recruitment website based on SSM

EventLoop learning

Completely solve the problem of Chinese garbled code in Web Engineering at one time
随机推荐
AXI总线
【问题解决】Tensorflow中run究竟运行了哪些节点?
Axi bus
How to use 200 lines of code to implement Scala's Object Converter
jvm 性能调优、监控工具 -- jps、jstack、jmap、jhat、jstat、hprof
buuctf misc 百里挑一
Istio微服务治理网格流量管理核心资源控制器详解
my.ini文件配置
招标公告:暨南大学附属第一医院Oracle数据库维保服务采购
微服务如何拆分
Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices
Crane: a new way of dealing with dictionary items and associated data
scrapy
Differences in perspectives of thinking
AXI總線
Pre training weekly issue 51: reconstruction pre training, zero sample automatic fine tuning, one click call opt
Openfeign service interface call
命令行编辑器 sed 基础用法总结
创建Deployment后,无法创建Pod问题处理
Infiltration learning diary day20