Skip to content

Commit 9ae8f57

Browse files
committed
1742. Maximum Number of Balls in a Box: AC
1 parent 2ca09cf commit 9ae8f57

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,3 +1312,4 @@ mod s1736_latest_time_by_replacing_hidden_digits;
13121312
mod s1737_change_minimum_characters_to_satisfy_one_of_three_conditions;
13131313
mod s1738_find_kth_largest_xor_coordinate_value;
13141314
mod s1739_building_boxes;
1315+
mod s1742_maximum_number_of_balls_in_a_box;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* [1742] Maximum Number of Balls in a Box
3+
*
4+
* You are working in a ball factory where you have n balls numbered from lowLimit up to highLimit inclusive (i.e., n == highLimit - lowLimit + 1), and an infinite number of boxes numbered from 1 to infinity.
5+
* Your job at this factory is to put each ball in the box with a number equal to the sum of digits of the ball's number. For example, the ball number 321 will be put in the box number 3 + 2 + 1 = 6 and the ball number 10 will be put in the box number 1 + 0 = 1.
6+
* Given two integers lowLimit and highLimit, return the number of balls in the box with the most balls.
7+
*
8+
* Example 1:
9+
*
10+
* Input: lowLimit = 1, highLimit = 10
11+
* Output: 2
12+
* Explanation:
13+
* Box Number: 1 2 3 4 5 6 7 8 9 10 11 ...
14+
* Ball Count: 2 1 1 1 1 1 1 1 1 0 0 ...
15+
* Box 1 has the most number of balls with 2 balls.
16+
* Example 2:
17+
*
18+
* Input: lowLimit = 5, highLimit = 15
19+
* Output: 2
20+
* Explanation:
21+
* Box Number: 1 2 3 4 5 6 7 8 9 10 11 ...
22+
* Ball Count: 1 1 1 1 2 2 1 1 1 0 0 ...
23+
* Boxes 5 and 6 have the most number of balls with 2 balls in each.
24+
*
25+
* Example 3:
26+
*
27+
* Input: lowLimit = 19, highLimit = 28
28+
* Output: 2
29+
* Explanation:
30+
* Box Number: 1 2 3 4 5 6 7 8 9 10 11 12 ...
31+
* Ball Count: 0 1 1 1 1 1 1 1 1 2 0 0 ...
32+
* Box 10 has the most number of balls with 2 balls.
33+
*
34+
*
35+
* Constraints:
36+
*
37+
* 1 <= lowLimit <= highLimit <= 10^5
38+
*
39+
*/
40+
pub struct Solution {}
41+
42+
// problem: https://leetcode.com/problems/maximum-number-of-balls-in-a-box/
43+
// discuss: https://leetcode.com/problems/maximum-number-of-balls-in-a-box/discuss/?currentPage=1&orderBy=most_votes&query=
44+
45+
// submission codes start here
46+
47+
impl Solution {
48+
pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 {
49+
let mut boxes = [0; 46];
50+
let sum_of_digits = |mut x: i32| {
51+
let mut s = 0;
52+
while x > 0 {
53+
s += x % 10;
54+
x /= 10;
55+
}
56+
s as usize
57+
};
58+
59+
(low_limit..=high_limit).for_each(|val| boxes[sum_of_digits(val)] += 1);
60+
boxes.iter().fold(0, |max_val, &x| max_val.max(x))
61+
}
62+
}
63+
64+
// submission codes end
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use std::result;
69+
70+
use super::*;
71+
72+
#[test]
73+
fn test_1742_example_1() {
74+
let low_limit = 1;
75+
let high_limit = 10;
76+
77+
let result = 2;
78+
79+
assert_eq!(Solution::count_balls(low_limit, high_limit), result);
80+
}
81+
82+
#[test]
83+
fn test_1742_example_2() {
84+
let low_limit = 5;
85+
let high_limit = 15;
86+
87+
let result = 2;
88+
89+
assert_eq!(Solution::count_balls(low_limit, high_limit), result);
90+
}
91+
92+
#[test]
93+
fn test_1742_example_3() {
94+
let low_limit = 19;
95+
let high_limit = 28;
96+
97+
let result = 2;
98+
99+
assert_eq!(Solution::count_balls(low_limit, high_limit), result);
100+
}
101+
}

0 commit comments

Comments
 (0)