File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
Expand file tree Collapse file tree 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
22
33[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
44
106710671395|[ Count Number of Teams] ( ./solutions/1395-count-number-of-teams.js ) |Medium|
106810681396|[ Design Underground System] ( ./solutions/1396-design-underground-system.js ) |Medium|
106910691397|[ Find All Good Strings] ( ./solutions/1397-find-all-good-strings.js ) |Hard|
1070+ 1399|[ Count Largest Group] ( ./solutions/1399-count-largest-group.js ) |Easy|
107010711400|[ Construct K Palindrome Strings] ( ./solutions/1400-construct-k-palindrome-strings.js ) |Medium|
107110721402|[ Reducing Dishes] ( ./solutions/1402-reducing-dishes.js ) |Hard|
107210731408|[ 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