当前位置:网站首页>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).
边栏推荐
猜你喜欢

复习专栏之---消息队列

【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
![[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

Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
![[TA frost wolf \u may- hundred talents plan] 1.2.3 MVP matrix operation](/img/4e/8cf60bc816441967c04f97c64685a1.png)
[TA frost wolf \u may- hundred talents plan] 1.2.3 MVP matrix operation

Web components series (VIII) -- custom component style settings

The programmer's girlfriend gave me a fatigue driving test

Binary tree god level traversal: Morris traversal

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)

程序员女友给我做了一个疲劳驾驶检测
随机推荐
171. excel table column No
[reach out to Party welfare] developer reload system sequence
5. [WebGIS practice] software operation - service release and permission management
Promql select time series
Cygwin的下载和安装配置
How keil displays Chinese annotations (simple with pictures)
Usage of AfxMessageBox and MessageBox
Quickly filter data such as clock in time and date: Excel filter to find whether a certain time point is within a certain time period
【EI会议】2022年国际土木与海洋工程联合会议(JCCME 2022)
392. judgment subsequence
Use selenium automated test tool to climb the enrollment score line and ranking of colleges and universities related to the college entrance examination
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
[ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle
208. implement trie (prefix tree)
【TA-霜狼_may-《百人計劃》】2.3 常用函數介紹
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
Error: plug ins declaring extensions or extension points must set the singleton directive to true
C语言的sem_t变量类型
【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
在 C 中声明函数之前调用函数会发生什么?