Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5a6857e

Browse files
add 1399
1 parentd1f353b commit5a6857e

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.HashMap;
5+
importjava.util.List;
6+
importjava.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+
publicclass_1399 {
37+
publicstaticclassSolution1 {
38+
publicintcountLargestGroup(intn) {
39+
Map<Integer,List<Integer>>map =newHashMap<>();
40+
for (inti =1;i <=n;i++) {
41+
intsumOfDigits =getSum(i);
42+
if (!map.containsKey(sumOfDigits)) {
43+
map.put(sumOfDigits,newArrayList<>());
44+
}
45+
map.get(sumOfDigits).add(i);
46+
}
47+
intmax =0;
48+
for (intkey :map.keySet()) {
49+
max =Math.max(max,map.get(key).size());
50+
}
51+
intcount =0;
52+
for (intkey :map.keySet()) {
53+
if (map.get(key).size() ==max) {
54+
count++;
55+
}
56+
}
57+
returncount;
58+
}
59+
60+
privateintgetSum(intnum) {
61+
intsum =0;
62+
while (num !=0) {
63+
sum +=num %10;
64+
num /=10;
65+
}
66+
returnsum;
67+
}
68+
}
69+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1399;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1399Test {
10+
privatestatic_1399.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1399.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(4,solution1.countLargestGroup(13));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(2,solution1.countLargestGroup(2));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(6,solution1.countLargestGroup(15));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(5,solution1.countLargestGroup(24));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp