Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5427f19

Browse files
committedApr 13, 2025
Add solution #1399
1 parent 996373f commit 5427f19

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,280 LeetCode solutions in JavaScript
1+
# 1,281 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1067,6 +1067,7 @@
10671067
1395|[Count Number of Teams](./solutions/1395-count-number-of-teams.js)|Medium|
10681068
1396|[Design Underground System](./solutions/1396-design-underground-system.js)|Medium|
10691069
1397|[Find All Good Strings](./solutions/1397-find-all-good-strings.js)|Hard|
1070+
1399|[Count Largest Group](./solutions/1399-count-largest-group.js)|Easy|
10701071
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
10711072
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
10721073
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|

‎solutions/1399-count-largest-group.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 1399. Count Largest Group
3+
* https://leetcode.com/problems/count-largest-group/
4+
* Difficulty: Easy
5+
*
6+
* You are given an integer n.
7+
*
8+
* Each number from 1 to n is grouped according to the sum of its digits.
9+
*
10+
* Return the number of groups that have the largest size.
11+
*/
12+
13+
/**
14+
* @param {number} n
15+
* @return {number}
16+
*/
17+
var countLargestGroup = function(n) {
18+
const map = new Map();
19+
let maxSize = 0;
20+
21+
for (let num = 1; num <= n; num++) {
22+
const digitSum = calculateDigitSum(num);
23+
const size = (map.get(digitSum) || 0) + 1;
24+
map.set(digitSum, size);
25+
maxSize = Math.max(maxSize, size);
26+
}
27+
28+
let result = 0;
29+
for (const size of map.values()) {
30+
if (size === maxSize) result++;
31+
}
32+
33+
return result;
34+
};
35+
36+
function calculateDigitSum(num) {
37+
let sum = 0;
38+
while (num > 0) {
39+
sum += num % 10;
40+
num = Math.floor(num / 10);
41+
}
42+
return sum;
43+
}

0 commit comments

Comments
 (0)
Please sign in to comment.