当前位置:网站首页>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==highLimitlowLimit+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;
    }
};
原网站

版权声明
本文为[AlbertOS]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250550166866.html