-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountLargestGroup1399.java
More file actions
62 lines (50 loc) · 1.5 KB
/
CountLargestGroup1399.java
File metadata and controls
62 lines (50 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.HashMap;
public class CountLargestGroup1399 {
/*
* You are given an integer n.
Each number from 1 to n is grouped according to the sum of its digits.
Return the number of groups that have the largest size.
*/
class Solution {
// Using Array
// Time Complexity: O(n)
// Space Complexity: O(1)
public int countLargestGroup(int n) {
int[] sums = new int[37];
for (int i = 1; i <= n; i++)
sums[digsum(i)]++;
int maxi = 0, count = 0;
for (int i : sums) {
if (i > maxi) {
maxi = i;
count = 1;
} else if (i == maxi) {
count++;
}
}
return count;
}
// Using HashMap
// Time Complexity: O(n)
// Space Complexity: O(n)
public int countLargestGroupUsingHashMap(int n) {
HashMap<Integer, Integer> mpp = new HashMap<>();
int maxi = 0, count = 0;
for (int i = 1; i <= n; i++) {
int x = digsum(i);
mpp.put(x, mpp.getOrDefault(x, 0) + 1);
maxi = Math.max(maxi, mpp.get(x));
}
for (int val : mpp.values()) if (val == maxi) count++;
return count;
}
private int digsum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
}
}