当前位置:网站首页>LeetCode 1399. Count the maximum number of groups
LeetCode 1399. Count the maximum number of groups
2022-07-01 03:52:00 【Daylight629】
1399. Count the number of the largest groups
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
Two 、 Method 1
Hash storage statistics is enough
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;
}
}
Complexity analysis
Time complexity : logarithm x The time to sum digits is O(log 10x)=O(logx), Therefore, the total time cost is O(nlogn), The time cost of selecting the largest element and traversing the hash table is O(n), Therefore, the time complexity gradually O(nlogn)+O(n)=O(nlogn).
Spatial complexity : Use hash table as secondary space ,n The number of digits is O(log 10 n)=O(logn), Every digit is [0,9] Between , Therefore, the maximum number of keys contained in the hash table is O(10logn)=O(logn), The asymptotic space complexity is O(logn).
边栏推荐
- 205. 同构字符串
- Jeecgboot output log, how to use @slf4j
- [small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
- 盘点华为云GaussDB(for Redis)六大秒级能力
- “目标检测“+“视觉理解“实现对输入图像的理解
- 205. isomorphic string
- 318. 最大单词长度乘积
- 206.反转链表
- Use of comment keyword in database
- [EI conference] 2022 international joint civil and Offshore Engineering Conference (jccme 2022)
猜你喜欢

25.K个一组翻转链表
![[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions](/img/be/325f78dee744138a865c13d2c20475.png)
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
![[TA frost wolf \u may- hundred people plan] 1.2.1 vector basis](/img/94/99090ea91082a385968e071ef3766c.png)
[TA frost wolf \u may- hundred people plan] 1.2.1 vector basis

Valentine's Day is nothing.

【TA-霜狼_may-《百人计划》】1.3纹理的秘密
![[ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle](/img/94/99090ea91082a385968e071ef3766c.png)
[ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle

Error: plug ins declaring extensions or extension points must set the singleton directive to true

Explain spark operation mode in detail (local+standalone+yarn)

熊市下的Coinbase:亏损、裁员、股价暴跌

报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
随机推荐
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
[shortcut key]
MFC窗口滚动条用法
409. 最长回文串
What happens when a function is called before it is declared in C?
242. valid Letter heteronyms
Redis(七)优化建议
Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
[EI search] important information conference of the 6th International Conference on materials engineering and advanced manufacturing technology (meamt 2022) in 2022 website: www.meamt Org meeting time
Addition without addition, subtraction, multiplication and division
Appium fundamentals of automated testing - basic principles of appium
The problem of integrating Alibaba cloud SMS: non static methods cannot be referenced from the static context
【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
Promql select time series
Complete knapsack problem
241. Design priorities for operational expressions
Libevent Library Learning
TS type gymnastics: illustrating a complex advanced type
392. judgment subsequence
171. excel table column No