当前位置:网站首页>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;
}
};
边栏推荐
- 如何用svn新建属于自己的分支
- test
- C get the version number of exe - file version and assembly version
- Evolution of Alibaba e-commerce architecture
- 差点被这波Handler 面试连环炮带走~
- Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
- 函数模板_类模板
- realsense d455 semantic_slam实现语义八叉树建图
- PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering
- Function template_ Class template
猜你喜欢
Summary of small problems in smartbugs installation
VectorDraw Web Library 10.10
npm install 报错 : gyp ERR! configure error
Chuantu microelectronics breaks through the high-end isolator analog chip market with ca-is3062w
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
Full range of isolator chips with integrated isolated power supply
权限、认证系统相关名词概念
三年营收连续下滑,天地壹号困在醋饮料里
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver
随机推荐
(tool class) use SecureCRT as the communication medium
[QT] shortcut key
[batch dos-cmd command - summary and summary] - commands related to Internet access and network communication (Ping, Telnet, NSLOOKUP, ARP, tracert, ipconfig)
57. 插入区间
Getting started with OpenMP
Accès à la boîte aux lettres du nom de domaine Lead à l'étranger
权限、认证系统相关名词概念
C#获取exe的版本号-文件版本and程序集版本
WinForm implementation window is always at the top level
STL tutorial 4- input / output stream and object serialization
This year, I graduated
单位转换-毫米转像素-像素转毫米
【QT】Qt 5 的程序:打印文档
Summary of small problems in smartbugs installation
OAuth 2.0一键登录那些事
Application scheme | application of Sichuan earth microelectronics ca-is398x in PLC field
Application of point cloud intelligent drawing in intelligent construction site
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
Sichuan earth microelectronics high performance, high integration and low cost isolated 485 transceiver