Skip to content

Commit aadfad1

Browse files
committed
feat: add solutions to lc problem: No.1742
No.1742.Maximum Number of Balls in a Box
1 parent ff02321 commit aadfad1

File tree

6 files changed

+204
-2
lines changed

6 files changed

+204
-2
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,85 @@
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```python
68-
68+
class Solution:
69+
def countBalls(self, lowLimit: int, highLimit: int) -> int:
70+
counter = [0] * 60
71+
for i in range(lowLimit, highLimit + 1):
72+
s = 0
73+
while i:
74+
s += (i % 10)
75+
i //= 10
76+
counter[s] += 1
77+
return max(counter)
6978
```
7079

7180
### **Java**
7281

7382
<!-- 这里可写当前语言的特殊实现逻辑 -->
7483

7584
```java
85+
class Solution {
86+
public int countBalls(int lowLimit, int highLimit) {
87+
int[] counter = new int[60];
88+
int ans = 0;
89+
for (int i = lowLimit; i <= highLimit; ++i) {
90+
int s = 0;
91+
int j = i;
92+
while (j > 0) {
93+
s += (j % 10);
94+
j /= 10;
95+
}
96+
++counter[s];
97+
ans = Math.max(ans, counter[s]);
98+
}
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int countBalls(int lowLimit, int highLimit) {
110+
vector<int> counter(60);
111+
int ans = 0;
112+
for (int i = lowLimit; i <= highLimit; ++i)
113+
{
114+
int s = 0, j = i;
115+
while (j)
116+
{
117+
s += (j % 10);
118+
j /= 10;
119+
}
120+
++counter[s];
121+
ans = max(ans, counter[s]);
122+
}
123+
return ans;
124+
}
125+
};
126+
```
76127
128+
### **Go**
129+
130+
```go
131+
func countBalls(lowLimit int, highLimit int) int {
132+
counter := make([]int, 60)
133+
ans := 0
134+
for i := lowLimit; i <= highLimit; i++ {
135+
s, j := 0, i
136+
for j > 0 {
137+
s += (j % 10)
138+
j /= 10
139+
}
140+
counter[s]++
141+
if counter[s] > ans {
142+
ans = counter[s]
143+
}
144+
}
145+
return ans
146+
}
77147
```
78148

79149
### **...**

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,83 @@ Box 10 has the most number of balls with 2 balls.
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def countBalls(self, lowLimit: int, highLimit: int) -> int:
62+
counter = [0] * 60
63+
for i in range(lowLimit, highLimit + 1):
64+
s = 0
65+
while i:
66+
s += (i % 10)
67+
i //= 10
68+
counter[s] += 1
69+
return max(counter)
6170
```
6271

6372
### **Java**
6473

6574
```java
75+
class Solution {
76+
public int countBalls(int lowLimit, int highLimit) {
77+
int[] counter = new int[60];
78+
int ans = 0;
79+
for (int i = lowLimit; i <= highLimit; ++i) {
80+
int s = 0;
81+
int j = i;
82+
while (j > 0) {
83+
s += (j % 10);
84+
j /= 10;
85+
}
86+
++counter[s];
87+
ans = Math.max(ans, counter[s]);
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int countBalls(int lowLimit, int highLimit) {
100+
vector<int> counter(60);
101+
int ans = 0;
102+
for (int i = lowLimit; i <= highLimit; ++i)
103+
{
104+
int s = 0, j = i;
105+
while (j)
106+
{
107+
s += (j % 10);
108+
j /= 10;
109+
}
110+
++counter[s];
111+
ans = max(ans, counter[s]);
112+
}
113+
return ans;
114+
}
115+
};
116+
```
66117
118+
### **Go**
119+
120+
```go
121+
func countBalls(lowLimit int, highLimit int) int {
122+
counter := make([]int, 60)
123+
ans := 0
124+
for i := lowLimit; i <= highLimit; i++ {
125+
s, j := 0, i
126+
for j > 0 {
127+
s += (j % 10)
128+
j /= 10
129+
}
130+
counter[s]++
131+
if counter[s] > ans {
132+
ans = counter[s]
133+
}
134+
}
135+
return ans
136+
}
67137
```
68138

69139
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int countBalls(int lowLimit, int highLimit) {
4+
vector<int> counter(60);
5+
int ans = 0;
6+
for (int i = lowLimit; i <= highLimit; ++i)
7+
{
8+
int s = 0, j = i;
9+
while (j)
10+
{
11+
s += (j % 10);
12+
j /= 10;
13+
}
14+
++counter[s];
15+
ans = max(ans, counter[s]);
16+
}
17+
return ans;
18+
}
19+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func countBalls(lowLimit int, highLimit int) int {
2+
counter := make([]int, 60)
3+
ans := 0
4+
for i := lowLimit; i <= highLimit; i++ {
5+
s, j := 0, i
6+
for j > 0 {
7+
s += (j % 10)
8+
j /= 10
9+
}
10+
counter[s]++
11+
if counter[s] > ans {
12+
ans = counter[s]
13+
}
14+
}
15+
return ans
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int countBalls(int lowLimit, int highLimit) {
3+
int[] counter = new int[60];
4+
int ans = 0;
5+
for (int i = lowLimit; i <= highLimit; ++i) {
6+
int s = 0;
7+
int j = i;
8+
while (j > 0) {
9+
s += (j % 10);
10+
j /= 10;
11+
}
12+
++counter[s];
13+
ans = Math.max(ans, counter[s]);
14+
}
15+
return ans;
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def countBalls(self, lowLimit: int, highLimit: int) -> int:
3+
counter = [0] * 60
4+
for i in range(lowLimit, highLimit + 1):
5+
s = 0
6+
while i:
7+
s += (i % 10)
8+
i //= 10
9+
counter[s] += 1
10+
return max(counter)

0 commit comments

Comments
 (0)