当前位置:网站首页>1742. 盒子中小球的最大数量
1742. 盒子中小球的最大数量
2022-06-25 06:41:00 【AlbertOS】
引入
你在一家生产小球的玩具厂工作,有 n n n 个小球,编号从 l o w L i m i t lowLimit lowLimit 开始,到 h i g h L i m i t highLimit highLimit 结束(包括 l o w L i m i t lowLimit lowLimit 和 h i g h L i m i t highLimit highLimit ,即 n = = h i g h L i m i t − l o w L i m i t + 1 ) n == highLimit - lowLimit + 1) n==highLimit−lowLimit+1)。另有无限数量的盒子,编号从 1 1 1 到 i n f i n i t y infinity infinity 。
你的工作是将每个小球放入盒子中,其中盒子的编号应当等于小球编号上每位数字的和。
例如,编号 321 的小球应当放入编号 3 + 2 + 1 = 6 的盒子,而编号 10 的小球应当放入编号 1 + 0 = 1 的盒子。
给你两个整数 l o w L i m i t lowLimit lowLimit 和 h i g h L i m i t highLimit highLimit ,返回放有最多小球的盒子中的小球数量。如果有多个盒子都满足放有最多小球,只需返回其中任一盒子的小球数量。
示例
输入: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 。
输入: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 。
输入: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 。
题解
我的写法比较暴力,小球最多有9999个,所以盒子标号最大为45,创建一个50的数组,直接将每个盒子的球数和求出来和result结果比较,取较大的那个,最后遍历完就是最大的盒子的球的数量。
class Solution {
public:
int countBalls(int lowLimit, int highLimit) {
vector<int> sums(50);
int result = -1;
for(int i = lowLimit;i<=highLimit;++i){
int temp = i ,sum = 0;
while(temp !=0){
sum += temp%10;
temp/=10;
}
sums[sum]++;
result = max(result,sums[sum]);
}
return result;
}
};
边栏推荐
- JMeter introduction practice ----- use of global variables and local variables
- Redis learning notes
- 50. Pow(x, n)-快速幂
- 个人域名和企业域名的区别
- GUI pull-down menu of unity3d evil door implementation dropdown design has no duplicate items
- NPM install reports an error: gyp err! configure error
- Keil and Proteus joint commissioning
- Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
- SQL solve select basic statement
- C#获取exe的版本号-文件版本and程序集版本
猜你喜欢

基于地面点稀少的LiDAR点云的茂密森林蓄积量估算

(tool class) use SecureCRT as the communication medium
![[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)](/img/fd/1d83ab3b318413ddf70f4e68b28cc3.png)
[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)

Full range of isolator chips with integrated isolated power supply

Sichuan earth microelectronics 8-channel isolated digital input receiver

Ns32f103c8t6 can perfectly replace stm32f103c8t6

Five causes of PCB board deformation and six solutions 2021-10-08

Sichuan Tuwei ca-is3105w fully integrated DC-DC converter

El input to add words to the tail

STL教程4-输入输出流和对象序列化
随机推荐
机器学习笔记 - 时间序列的线性回归
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
WinForm实现窗口始终在顶层
OAuth 2.0一键登录那些事
Terms and concepts related to authority and authentication system
What if there is no point in data visualization?
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
My debut is finished!
权限、认证系统相关名词概念
opencv最小值滤波(不局限于图像)
[batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)
ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
Sichuan earth microelectronics 8-channel isolated digital input receiver
Evolution of Alibaba e-commerce architecture
指南针可以开股票账户吗?安全吗?
单位转换-毫米转像素-像素转毫米
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
2265. 统计值等于子树平均值的节点数
“空间转换”显著提升陡崖点云的地面点提取质量
搞清信息化是什么,让企业转型升级走上正确的道路