当前位置:网站首页>Day10 daily 3 questions (2): count the number of the largest groups
Day10 daily 3 questions (2): count the number of the largest groups
2022-06-26 16:36:00 【Programmed ape without hair loss 2】
subject :
Give you an integer n . Please find out from 1 To n Each integer of 10 Hexadecimal represents the sum of digits under ( Add the numbers on each digit ), Then put the digits and equal numbers in the same group .
Please count the number of numbers in each group , And return the number of groups with the largest number of numbers .
Example 1:
Input :n = 13
Output :4
explain : All in all 9 A set of , take 1 To 13 After summing by digits, these groups are :
[1,10],[2,11],[3,12],[4,13],[5],[6],[7],[8],[9]. All in all 4 Groups have the most numbers in parallel .
Example 2:
Input :n = 2
Output :2
explain : All in all 2 Size is 1 Group [1],[2].
Example 3:
Input :n = 15
Output :6
Example 4:
Input :n = 24
Output :5
Tips :
1 <= n <= 10^4
Ideas :
about [1, n] Every integer in i, We can calculate its digits and s_i . Create a hash map from digit sum to original digit , For every number i, Make key s_i The corresponding value increases by one . Then we find the largest value in the set of values m, Then traverse the hash table , The statistical value is m The number of .
java Code :
class Solution {
public int countLargestGroup(int n) {
Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
int maxValue = 0;
for (int i = 1; i <= n; ++i) {
int key = 0, i0 = i;
while (i0 != 0) {
key += i0 % 10;
i0 /= 10;
}
hashMap.put(key, hashMap.getOrDefault(key, 0) + 1);
maxValue = Math.max(maxValue, hashMap.get(key));
}
int count = 0;
for (Map.Entry<Integer, Integer> kvpair : hashMap.entrySet()) {
if (kvpair.getValue() == maxValue) {
++count;
}
}
return count;
}
}
边栏推荐
- TCP拥塞控制详解 | 1. 概述
- Natural language inference with attention and fine tuning Bert pytorch
- day10每日3题(3):数组中的字符串匹配
- JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序
- 了解下常见的函数式接口
- [learn FPGA programming from scratch -46]: Vision - development and technological progress of integrated circuits
- JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
- [机缘参悟-31]:鬼谷子-抵巇[xī]篇-危机是危险与机会并存
- Several forms of buffer in circuit
- Least squares system identification class II: recursive least squares
猜你喜欢

Big talk Domain Driven Design -- presentation layer and others

我把它当副业月入3万多,新手月入过万的干货分享!
![[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity](/img/e8/9c5f1658a252c3c80503b5021917f6.jpg)
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity
Scala Foundation (2): variables et types de données

Research on natural transition dubbing processing scheme based on MATLAB

5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?

Structure the graduation project of actual combat camp

I regard it as a dry product with a monthly income of more than 30000 yuan for sidelines and more than 10000 yuan for novices!
![[机缘参悟-31]:鬼谷子-抵巇[xī]篇-危机是危险与机会并存](/img/e8/9c5f1658a252c3c80503b5021917f6.jpg)
[机缘参悟-31]:鬼谷子-抵巇[xī]篇-危机是危险与机会并存

What does the inner structure of the neural network "alchemy furnace" look like? An interpretation of the thesis by the doctor of Oxford University
随机推荐
Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读
GUI+SQLServer考试系统
JS tutorial - printing stickers / labels using the electronjs desktop application
Knowing these commands allows you to master shell's own tools
Arduino UNO + DS1302简单获取时间并串口打印
day10每日3题(2):统计最大组的数目
Greenplum数据库故障分析——semop(id=2000421076,num=11) failed: invalid argument
【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
In a bad mood, I just write code like this
TCP congestion control details | 1 summary
Make up the weakness - Open Source im project openim about initialization / login / friend interface document introduction
网页课程设计大作业——华山旅游网
【力扣刷题】11.盛最多水的容器//42.接雨水
When a programmer is disturbed 10 times a day, the consequences are amazing!
R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
股票开户优惠链接,我如何才能得到?在线开户安全么?
JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序