Skip to content

Commit 47462d9

Browse files
authored
feat: add solutions to lc problem: No.1742 (#4058)
No.1742.Maximum Number of Balls in a Box
1 parent 697263f commit 47462d9

File tree

5 files changed

+174
-6
lines changed

5 files changed

+174
-6
lines changed

solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md

+64-3
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ tags:
7676

7777
### 方法一:数组 + 模拟
7878

79-
观察题目的数据范围,小球的编号最大不超过 $10^5$,那么每个编号的各个位数之和的最大值小于 $50$。因此,我们可以直接开一个长度为 $50$ 的数组 $cnt$ 来统计每个编号的各个位数之和的数量。
79+
观察题目的数据范围,小球的编号最大不超过 $10^5$,那么每个编号的各个位数之和的最大值小于 $50$。因此,我们可以直接开一个长度为 $50$ 的数组 $\textit{cnt}$ 来统计每个编号的各个位数之和的数量。
8080

81-
答案就是数组 $cnt$ 中的最大值。
81+
答案就是数组 $\textit{cnt}$ 中的最大值。
8282

83-
时间复杂度 $O(n \times \log_{10}m)$。其中 $n = highLimit - lowLimit + 1$,而 $m = highLimit$。
83+
时间复杂度 $O(n \times \log_{10}m)$。其中 $n = \textit{highLimit} - \textit{lowLimit} + 1$,而 $m = \textit{highLimit}$。
8484

8585
<!-- tabs:start -->
8686

@@ -172,6 +172,67 @@ function countBalls(lowLimit: number, highLimit: number): number {
172172
}
173173
```
174174

175+
#### Rust
176+
177+
```rust
178+
impl Solution {
179+
pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 {
180+
let mut cnt = vec![0; 50];
181+
for x in low_limit..=high_limit {
182+
let mut y = 0;
183+
let mut n = x;
184+
while n > 0 {
185+
y += n % 10;
186+
n /= 10;
187+
}
188+
cnt[y as usize] += 1;
189+
}
190+
*cnt.iter().max().unwrap()
191+
}
192+
}
193+
```
194+
195+
#### JavaScript
196+
197+
```js
198+
/**
199+
* @param {number} lowLimit
200+
* @param {number} highLimit
201+
* @return {number}
202+
*/
203+
var countBalls = function (lowLimit, highLimit) {
204+
const cnt = Array(50).fill(0);
205+
for (let i = lowLimit; i <= highLimit; ++i) {
206+
let y = 0;
207+
for (let x = i; x; x = Math.floor(x / 10)) {
208+
y += x % 10;
209+
}
210+
++cnt[y];
211+
}
212+
return Math.max(...cnt);
213+
};
214+
```
215+
216+
#### C#
217+
218+
```cs
219+
public class Solution {
220+
public int CountBalls(int lowLimit, int highLimit) {
221+
int[] cnt = new int[50];
222+
for (int x = lowLimit; x <= highLimit; x++) {
223+
int y = 0;
224+
int n = x;
225+
while (n > 0) {
226+
y += n % 10;
227+
n /= 10;
228+
}
229+
cnt[y]++;
230+
}
231+
return cnt.Max();
232+
}
233+
}
234+
```
235+
175236
<!-- tabs:end -->
176237

177238
<!-- solution:end -->

solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md

+64-3
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ Box 10 has the most number of balls with 2 balls.
7474

7575
### Solution 1: Array + Simulation
7676

77-
Observing the data range of the problem, the maximum number of the ball does not exceed $10^5$, so the maximum value of the sum of each digit of the number is less than $50$. Therefore, we can directly create an array $cnt$ with a length of $50$ to count the number of each digit sum of each number.
77+
Observing the problem's data range, the maximum number of balls does not exceed $10^5$, so the maximum sum of the digits of each number is less than $50$. Therefore, we can directly create an array $\textit{cnt}$ of length $50$ to count the number of occurrences of each digit sum.
7878

79-
The answer is the maximum value in the array $cnt$.
79+
The answer is the maximum value in the array $\textit{cnt}$.
8080

81-
The time complexity is $O(n \times \log_{10}m)$. Here, $n = highLimit - lowLimit + 1$, and $m = highLimit$.
81+
The time complexity is $O(n \times \log_{10}m)$. Here, $n = \textit{highLimit} - \textit{lowLimit} + 1$, and $m = \textit{highLimit}$.
8282

8383
<!-- tabs:start -->
8484

@@ -170,6 +170,67 @@ function countBalls(lowLimit: number, highLimit: number): number {
170170
}
171171
```
172172

173+
#### Rust
174+
175+
```rust
176+
impl Solution {
177+
pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 {
178+
let mut cnt = vec![0; 50];
179+
for x in low_limit..=high_limit {
180+
let mut y = 0;
181+
let mut n = x;
182+
while n > 0 {
183+
y += n % 10;
184+
n /= 10;
185+
}
186+
cnt[y as usize] += 1;
187+
}
188+
*cnt.iter().max().unwrap()
189+
}
190+
}
191+
```
192+
193+
#### JavaScript
194+
195+
```js
196+
/**
197+
* @param {number} lowLimit
198+
* @param {number} highLimit
199+
* @return {number}
200+
*/
201+
var countBalls = function (lowLimit, highLimit) {
202+
const cnt = Array(50).fill(0);
203+
for (let i = lowLimit; i <= highLimit; ++i) {
204+
let y = 0;
205+
for (let x = i; x; x = Math.floor(x / 10)) {
206+
y += x % 10;
207+
}
208+
++cnt[y];
209+
}
210+
return Math.max(...cnt);
211+
};
212+
```
213+
214+
#### C#
215+
216+
```cs
217+
public class Solution {
218+
public int CountBalls(int lowLimit, int highLimit) {
219+
int[] cnt = new int[50];
220+
for (int x = lowLimit; x <= highLimit; x++) {
221+
int y = 0;
222+
int n = x;
223+
while (n > 0) {
224+
y += n % 10;
225+
n /= 10;
226+
}
227+
cnt[y]++;
228+
}
229+
return cnt.Max();
230+
}
231+
}
232+
```
233+
173234
<!-- tabs:end -->
174235

175236
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int CountBalls(int lowLimit, int highLimit) {
3+
int[] cnt = new int[50];
4+
for (int x = lowLimit; x <= highLimit; x++) {
5+
int y = 0;
6+
int n = x;
7+
while (n > 0) {
8+
y += n % 10;
9+
n /= 10;
10+
}
11+
cnt[y]++;
12+
}
13+
return cnt.Max();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number} lowLimit
3+
* @param {number} highLimit
4+
* @return {number}
5+
*/
6+
var countBalls = function (lowLimit, highLimit) {
7+
const cnt = Array(50).fill(0);
8+
for (let i = lowLimit; i <= highLimit; ++i) {
9+
let y = 0;
10+
for (let x = i; x; x = Math.floor(x / 10)) {
11+
y += x % 10;
12+
}
13+
++cnt[y];
14+
}
15+
return Math.max(...cnt);
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 {
3+
let mut cnt = vec![0; 50];
4+
for x in low_limit..=high_limit {
5+
let mut y = 0;
6+
let mut n = x;
7+
while n > 0 {
8+
y += n % 10;
9+
n /= 10;
10+
}
11+
cnt[y as usize] += 1;
12+
}
13+
*cnt.iter().max().unwrap()
14+
}
15+
}

0 commit comments

Comments
 (0)