当前位置:网站首页>[daily 3 questions (3)] maximum number of balls in the box
[daily 3 questions (3)] maximum number of balls in the box
2022-06-27 13:46:00 【Programmed ape without hair loss 2】
subject :
You work in a toy factory that makes balls , Yes n A little ball , Number from lowLimit Start , To highLimit end ( Include lowLimit and highLimit , namely n == highLimit - lowLimit + 1). And there's an infinite number of boxes , Number from 1 To 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 lowLimit and 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 1:
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 .
Example 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 .
Example 3:
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 .
Tips :
1 <= lowLimit <= highLimit <= 10^5
Ideas :
Focus on the sum of digits , Remainder 10, Divide 10;
java Code :
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;
}
}
边栏推荐
猜你喜欢

赛迪顾问发布《“十四五” 关键应用领域之数据库市场研究报告》(附下载)

新华三的千亿企业梦,还得靠吃ICT老本来实现?

每日刷題記錄 (六)

PLM还能怎么用?

Does Xinhua San still have to rely on ICT to realize its 100 billion enterprise dream?

High efficiency exponentiation

嵌入式开发:嵌入式基础——回调函数

How to set the compatibility mode of 360 speed browser

以前国产手机高傲定价扬言消费者爱买不买,现在猛降两千求售

Cesium realizes satellite orbit detour
随机推荐
dynamic programming
MySQL 索引及其分类
【业务安全-01】业务安全概述及测试流程
A statistical problem of shell script
《预训练周刊》第51期:重构预训练、零样本自动微调、一键调用OPT
PLM还能怎么用?
[安洵杯 2019]Attack
Axi bus
After 2 years of outsourcing, I finally landed! Record my ByteDance 3 rounds of interviews, hope to help you!
IJCAI 2022 | 用一行代码大幅提升零样本学习方法效果,南京理工&牛津提出即插即用分类器模块
A method to realize automatic renaming of pictures uploaded by WordPress
解析Activity启动-生命周期角度
Interviewer: do you understand redis' shared object pool?
Shell 简明教程
Crane: a new way of dealing with dictionary items and associated data
Infiltration learning diary day20
每日刷題記錄 (六)
enable_if
[day 27] given an integer n, print out the full permutation from 1 to n | Full Permutation template
Kyndryl partnered with Oracle and Veritas