File tree 2 files changed +46
-1
lines changed 2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,513 LeetCode solutions in JavaScript
1
+ # 1,514 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1341
1341
1737|[ Change Minimum Characters to Satisfy One of Three Conditions] ( ./solutions/1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js ) |Medium|
1342
1342
1738|[ Find Kth Largest XOR Coordinate Value] ( ./solutions/1738-find-kth-largest-xor-coordinate-value.js ) |Medium|
1343
1343
1739|[ Building Boxes] ( ./solutions/1739-building-boxes.js ) |Hard|
1344
+ 1742|[ Maximum Number of Balls in a Box] ( ./solutions/1742-maximum-number-of-balls-in-a-box.js ) |Easy|
1344
1345
1748|[ Sum of Unique Elements] ( ./solutions/1748-sum-of-unique-elements.js ) |Easy|
1345
1346
1749|[ Maximum Absolute Sum of Any Subarray] ( ./solutions/1749-maximum-absolute-sum-of-any-subarray.js ) |Medium|
1346
1347
1752|[ Check if Array Is Sorted and Rotated] ( ./solutions/1752-check-if-array-is-sorted-and-rotated.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1742. Maximum Number of Balls in a Box
3
+ * https://leetcode.com/problems/maximum-number-of-balls-in-a-box/
4
+ * Difficulty: Easy
5
+ *
6
+ * You are working in a ball factory where you have n balls numbered from lowLimit up to
7
+ * highLimit inclusive (i.e., n == highLimit - lowLimit + 1), and an infinite number of
8
+ * boxes numbered from 1 to infinity.
9
+ *
10
+ * Your job at this factory is to put each ball in the box with a number equal to the sum
11
+ * of digits of the ball's number. For example, the ball number 321 will be put in the
12
+ * box number 3 + 2 + 1 = 6 and the ball number 10 will be put in the box number 1 + 0 = 1.
13
+ *
14
+ * Given two integers lowLimit and highLimit, return the number of balls in the box with
15
+ * the most balls.
16
+ */
17
+
18
+ /**
19
+ * @param {number } lowLimit
20
+ * @param {number } highLimit
21
+ * @return {number }
22
+ */
23
+ var countBalls = function ( lowLimit , highLimit ) {
24
+ const boxCounts = new Map ( ) ;
25
+ let result = 0 ;
26
+
27
+ for ( let ball = lowLimit ; ball <= highLimit ; ball ++ ) {
28
+ const box = sumDigits ( ball ) ;
29
+ const count = ( boxCounts . get ( box ) || 0 ) + 1 ;
30
+ boxCounts . set ( box , count ) ;
31
+ result = Math . max ( result , count ) ;
32
+ }
33
+
34
+ return result ;
35
+
36
+ function sumDigits ( num ) {
37
+ let sum = 0 ;
38
+ while ( num > 0 ) {
39
+ sum += num % 10 ;
40
+ num = Math . floor ( num / 10 ) ;
41
+ }
42
+ return sum ;
43
+ }
44
+ } ;
You can’t perform that action at this time.
0 commit comments