File tree 2 files changed +45
-1
lines changed
2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,280 LeetCode solutions in JavaScript
1
+ # 1,281 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1067
1067
1395|[ Count Number of Teams] ( ./solutions/1395-count-number-of-teams.js ) |Medium|
1068
1068
1396|[ Design Underground System] ( ./solutions/1396-design-underground-system.js ) |Medium|
1069
1069
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|
1070
1071
1400|[ Construct K Palindrome Strings] ( ./solutions/1400-construct-k-palindrome-strings.js ) |Medium|
1071
1072
1402|[ Reducing Dishes] ( ./solutions/1402-reducing-dishes.js ) |Hard|
1072
1073
1408|[ String Matching in an Array] ( ./solutions/1408-string-matching-in-an-array.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments