Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5a6857e

Browse files
add 1399
1 parent d1f353b commit 5a6857e

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1400|[Construct K Palindrome Strings](https://leetcode.com/problems/construct-k-palindrome-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1400.java) | |Medium|Greedy|
12+
|1399|[Count Largest Group](https://leetcode.com/problems/count-largest-group/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1399.java) | |Easy|Array|
1213
|1396|[Design Underground System](https://leetcode.com/problems/design-underground-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1396.java) | |Medium|Design|
1314
|1395|[Count Number of Teams](https://leetcode.com/problems/count-number-of-teams/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1395.java) | |Medium|Array|
1415
|1394|[Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1394.java) | |Easy|Array|
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* 1399. Count Largest Group
10+
*
11+
* Given an integer n. Each number from 1 to n is grouped according to the sum of its digits.
12+
* Return how many groups have the largest size.
13+
*
14+
* Example 1:
15+
* Input: n = 13
16+
* Output: 4
17+
* Explanation: There are 9 groups in total, they are grouped according sum of its digits of numbers from 1 to 13:
18+
* [1,10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9]. There are 4 groups with largest size.
19+
*
20+
* Example 2:
21+
* Input: n = 2
22+
* Output: 2
23+
* Explanation: There are 2 groups [1], [2] of size 1.
24+
*
25+
* Example 3:
26+
* Input: n = 15
27+
* Output: 6
28+
*
29+
* Example 4:
30+
* Input: n = 24
31+
* Output: 5
32+
*
33+
* Constraints:
34+
* 1 <= n <= 10^4
35+
* */
36+
public class _1399 {
37+
public static class Solution1 {
38+
public int countLargestGroup(int n) {
39+
Map<Integer, List<Integer>> map = new HashMap<>();
40+
for (int i = 1; i <= n; i++) {
41+
int sumOfDigits = getSum(i);
42+
if (!map.containsKey(sumOfDigits)) {
43+
map.put(sumOfDigits, new ArrayList<>());
44+
}
45+
map.get(sumOfDigits).add(i);
46+
}
47+
int max = 0;
48+
for (int key : map.keySet()) {
49+
max = Math.max(max, map.get(key).size());
50+
}
51+
int count = 0;
52+
for (int key : map.keySet()) {
53+
if (map.get(key).size() == max) {
54+
count++;
55+
}
56+
}
57+
return count;
58+
}
59+
60+
private int getSum(int num) {
61+
int sum = 0;
62+
while (num != 0) {
63+
sum += num % 10;
64+
num /= 10;
65+
}
66+
return sum;
67+
}
68+
}
69+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1399;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1399Test {
10+
private static _1399.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1399.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.countLargestGroup(13));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(2, solution1.countLargestGroup(2));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(6, solution1.countLargestGroup(15));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(5, solution1.countLargestGroup(24));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)