Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1676eee

Browse files
committed
Add solution #1742
1 parent 2c216ec commit 1676eee

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-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,513 LeetCode solutions in JavaScript
1+
# 1,514 LeetCode solutions in JavaScript
22

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

@@ -1341,6 +1341,7 @@
13411341
1737|[Change Minimum Characters to Satisfy One of Three Conditions](./solutions/1737-change-minimum-characters-to-satisfy-one-of-three-conditions.js)|Medium|
13421342
1738|[Find Kth Largest XOR Coordinate Value](./solutions/1738-find-kth-largest-xor-coordinate-value.js)|Medium|
13431343
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|
13441345
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13451346
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
13461347
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
};

0 commit comments

Comments
 (0)