当前位置:网站首页>1742. maximum number of small balls in the box
1742. maximum number of small balls in the box
2022-06-25 07:50:00 【AlbertOS】
introduce
You work in a toy factory that makes balls , Yes n n n A little ball , Number from l o w L i m i t lowLimit lowLimit Start , To h i g h L i m i t highLimit highLimit end ( Include l o w L i m i t lowLimit lowLimit and h i g h L i m i t highLimit highLimit , namely 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). And there's an infinite number of boxes , Number from 1 1 1 To i n f i n i t y infinity infinity .
Your job is to put each ball in a box , The number of the box should be equal to the sum of each number on the ball number .
for example , Number 321 The ball should be numbered 3 + 2 + 1 = 6 Box , And number 10 The ball should be numbered 1 + 0 = 1 Box .
Here are two integers l o w L i m i t lowLimit lowLimit and h i g h L i m i t highLimit highLimit , Returns the number of balls in the box with the largest number of balls . If more than one box is enough to hold the most balls , Just return the number of balls in any box .
Example
Input :lowLimit = 1, highLimit = 10
Output :2
explain :
Box number :1 2 3 4 5 6 7 8 9 10 11 …
Number of balls :2 1 1 1 1 1 1 1 1 0 0 …
Number 1 I have the most balls in my box , The number of balls is 2 .
Input :lowLimit = 5, highLimit = 15
Output :2
explain :
Box number :1 2 3 4 5 6 7 8 9 10 11 …
Number of balls :1 1 1 1 2 2 1 1 1 0 0 …
Number 5 and 6 I have the most balls in my box , How many balls are in each box 2 .
Input :lowLimit = 19, highLimit = 28
Output :2
explain :
Box number :1 2 3 4 5 6 7 8 9 10 11 12 …
Number of balls :0 1 1 1 1 1 1 1 1 2 0 0 …
Number 10 I have the most balls in my box , The number of balls is 2 .
Answer key
My writing is more violent , Most of the small balls 9999 individual , So the maximum box label is 45, Create a 50 Array of , Sum the number of balls in each box result Results comparison , Take the bigger one , The last traversal is the number of balls in the largest box .
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;
}
};
边栏推荐
猜你喜欢

函数模板_类模板

一次弄清楚 Handler 可能导致的内存泄漏和解决办法

OAuth 2.0一键登录那些事

opencv最小值滤波(不局限于图像)

How to use ad wiring for PCB design?

Modular programming of LCD1602 LCD controlled by single chip microcomputer

FairMOT yolov5s转onnx

ts环境搭建

【蒸馏】PointDistiller: Structured Knowledge DistillationTowards Efficient and Compact 3D Detection

How to resize an image in C #
随机推荐
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
WinForm implementation window is always at the top level
【QT】Qt 5 的程序:打印文档
Take you through the normalization flow of GaN
C WinForm panel custom picture and text
Cifar-10 dataset application: quick start data enhancement method mixup significantly improves image recognition accuracy
Leetcode daily question - 515 Find the maximum value in each tree row
What are the benefits of reserving process edges for PCB production? 2021-10-25
用函数的递归来解决几道有趣的题
C#中如何调整图像大小
Tips on how to design soft and hard composite boards ~ 22021/11/22
Collection of common terms and meanings in forestry investigation based on lidar
C# 读取web上的xml
(tool class) use SecureCRT as the communication medium
搞清信息化是什么,让企业转型升级走上正确的道路
Pit encountered by pytorch: why can't l1loss decrease during model training?
Requirements for Power PCB circuit board design 2021-11-09
2160. 拆分数位后四位数字的最小和
Getting started with OpenMP
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘