当前位置:网站首页>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;
}
}
边栏推荐
- Science | 红树林中发现的巨型细菌挑战传统无核膜观念
- 固件供应链公司Binarly获得WestWave Capital和Acrobator Ventures的360万美元投资
- Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
- What does the inner structure of the neural network "alchemy furnace" look like? An interpretation of the thesis by the doctor of Oxford University
- TCP拥塞控制详解 | 1. 概述
- 若依微服务特殊字符串被过滤的解决办法
- Which position does Anxin securities rank? Is it safe to open an account?
- Arduino uno + DS1302 simple time acquisition and serial port printing
- 【力扣刷题】单调栈:84. 柱状图中最大的矩形
- 网页课程设计大作业——华山旅游网
猜你喜欢
![[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)](/img/75/2fb1a4e6215e404df34849e9e4f21a.png)
[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)

Develop operator based on kubebuilder (for getting started)

牛客小白月赛50

【力扣刷题】11.盛最多水的容器//42.接雨水

用Attention和微调BERT进行自然语言推断-PyTorch

当一个程序员一天被打扰 10 次,后果很惊人!

无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...

神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读

心情不好,我就这样写代码

对话长安马自达高层,全新产品将在Q4发布,空间与智能领跑日系
随机推荐
This year, the AI score of college entrance examination English is 134. The research of Fudan Wuda alumni is interesting
proxy
当一个程序员一天被打扰 10 次,后果很惊人!
Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
Which position does Anxin securities rank? Is it safe to open an account?
R language uses cor function to calculate the correlation matrix for correlation analysis, uses corrgram package to visualize the correlation matrix, reorders rows and columns using principal componen
MHA switching (recommended operation process)
JS教程之Electron.js设计强大的多平台桌面应用程序的好工具
用Attention和微调BERT进行自然语言推断-PyTorch
day10每日3题(2):统计最大组的数目
国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
1-12Vmware新增SSH功能
How to separate jar packages and resource files according to packaging?
LeetCode 单周赛298,前三题
When a programmer is disturbed 10 times a day, the consequences are amazing!
Learn about common functional interfaces
Natural language inference with attention and fine tuning Bert pytorch
Notes on key review of software engineering at the end of the term
Leetcode one week race 298, first three questions
Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser