当前位置:网站首页>【每日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;
}
}
边栏推荐
- How to use 200 lines of code to implement Scala's Object Converter
- 每日刷题记录 (六)
- A method to realize automatic renaming of pictures uploaded by WordPress
- CMOS level circuit analysis
- Summary and Thinking on interface test automation
- 一道shell脚本的统计题
- 快速搭建一个自己的访问国外网站,搭建ss并开启bbr快速上网
- 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
- 招标公告:暨南大学附属第一医院Oracle数据库维保服务采购
- Details of istio micro service governance grid traffic management core resource controller
猜你喜欢

Teach you how to build a permanent personal server!

CMOS级电路分析

Postman如何设置成中文?(汉化)

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

每日刷題記錄 (六)

NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络

Cool in summer

Using FRP tool to realize intranet penetration

How to solve the problem of missing language bar in win10 system

以前国产手机高傲定价扬言消费者爱买不买,现在猛降两千求售
随机推荐
招标公告:上海市研发公共服务平台管理中心Oracle一体机软硬件维保项目
AGCO AI frontier promotion (6.27)
How ASP connects Excel
Hue new account error reporting solution
PCL库——报错解决:安装时遇到的cmake与anaconda的冲突问题
万物互联时代到来,锐捷发布场景化无线零漫游方案
What is low code for digital Nova? What is no code
Explore tidb lightning source code to solve the found bugs
Prometheus 2.26.0 新特性
面试官:Redis的共享对象池了解吗?
Implementation of recruitment website based on SSM
SFINAE
Shell concise tutorial
my.ini文件配置
Crane: a new way of dealing with dictionary items and associated data
Deploy redis sentinel mode using bitnamiredis Sentinel
美国芯片再遭重击,继Intel后又一家芯片企业将被中国芯片超越
深入理解位运算
Details of istio micro service governance grid traffic management core resource controller
Summary and Thinking on interface test automation