Skip to content

Commit 7f17820

Browse files
committed
feat: update solutions to lc problem: No.1742
No.1742.Maximum Number of Balls in a Box
1 parent 0dd1c7d commit 7f17820

File tree

7 files changed

+93
-111
lines changed

7 files changed

+93
-111
lines changed

solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
**方法二:动态规划**
6161

62-
我们设 $f[i][j]$ 表示第 $i$ 天交易完后的最大利润,其中 $j$ 表示当前是否持有股票,持有股票时 $j=0$,不持有股票时 $j=1$。
62+
我们设 $f[i][j]$ 表示第 $i$ 天交易完后的最大利润,其中 $j$ 表示当前是否持有股票,持有股票时 $j=0$,不持有股票时 $j=1$。初始状态为 $f[0][0]=-prices[0]$,其余状态均为 $0$。
6363

6464
如果当前持有股票,那么可能是前一天就持有股票,今天什么都不做,即 $f[i][0]=f[i-1][0]$;也可能是前一天不持有股票,今天买入股票,即 $f[i][0]=f[i-1][1]-prices[i]$。
6565

@@ -74,8 +74,6 @@ f[i][1]=\max(f[i-1][1],f[i-1][0]+prices[i])
7474
\end{cases}
7575
$$
7676

77-
初始状态为 $f[0][0]=-prices[0]$,$f[0][1]=0$。
78-
7977
最终的答案即为 $f[n-1][1]$,其中 $n$ 为数组 `prices` 的长度。
8078

8179
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `prices` 的长度。

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:数组 + 模拟**
62+
63+
根据题意我们知道,小球的编号最大不超过 $10^5$,每个编号的各个位数之和的最大值小于 $50$,因此,我们可以用一个长度为 $50$ 的数组来统计每个编号的各个位数之和的数量。
64+
65+
答案就是这个数组中的最大值。
66+
67+
时间复杂度 $O(n \times \log_{10}m)$。其中 $n = highLimit - lowLimit + 1$,而 $m = highLimit$。
68+
6169
<!-- tabs:start -->
6270

6371
### **Python3**
@@ -67,14 +75,14 @@
6775
```python
6876
class Solution:
6977
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)
78+
cnt = [0] * 50
79+
for x in range(lowLimit, highLimit + 1):
80+
y = 0
81+
while x:
82+
y += x % 10
83+
x //= 10
84+
cnt[y] += 1
85+
return max(cnt)
7886
```
7987

8088
### **Java**
@@ -84,19 +92,15 @@ class Solution:
8492
```java
8593
class Solution {
8694
public int countBalls(int lowLimit, int highLimit) {
87-
int[] counter = new int[60];
88-
int ans = 0;
95+
int[] cnt = new int[50];
8996
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;
97+
int x = i, y = 0;
98+
for (; x > 0; x /= 10) {
99+
y += x % 10;
95100
}
96-
++counter[s];
97-
ans = Math.max(ans, counter[s]);
101+
++cnt[y];
98102
}
99-
return ans;
103+
return Arrays.stream(cnt).max().getAsInt();
100104
}
101105
}
102106
```
@@ -107,16 +111,14 @@ class Solution {
107111
class Solution {
108112
public:
109113
int countBalls(int lowLimit, int highLimit) {
110-
vector<int> counter(60);
114+
int cnt[50] = {0};
111115
int ans = 0;
112116
for (int i = lowLimit; i <= highLimit; ++i) {
113-
int s = 0, j = i;
114-
while (j) {
115-
s += (j % 10);
116-
j /= 10;
117+
int x = i, y = 0;
118+
for (; x; x /= 10) {
119+
y += x % 10;
117120
}
118-
++counter[s];
119-
ans = max(ans, counter[s]);
121+
ans = max(ans, ++cnt[y]);
120122
}
121123
return ans;
122124
}
@@ -126,21 +128,19 @@ public:
126128
### **Go**
127129
128130
```go
129-
func countBalls(lowLimit int, highLimit int) int {
130-
counter := make([]int, 60)
131-
ans := 0
131+
func countBalls(lowLimit int, highLimit int) (ans int) {
132+
cnt := [50]int{}
132133
for i := lowLimit; i <= highLimit; i++ {
133-
s, j := 0, i
134-
for j > 0 {
135-
s += (j % 10)
136-
j /= 10
134+
x, y := i, 0
135+
for ; x > 0; x /= 10 {
136+
y += x % 10
137137
}
138-
counter[s]++
139-
if counter[s] > ans {
140-
ans = counter[s]
138+
cnt[y]++
139+
if ans < cnt[y] {
140+
ans = cnt[y]
141141
}
142142
}
143-
return ans
143+
return
144144
}
145145
```
146146

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

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,30 @@ Box 10 has the most number of balls with 2 balls.
5959
```python
6060
class Solution:
6161
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)
62+
cnt = [0] * 50
63+
for x in range(lowLimit, highLimit + 1):
64+
y = 0
65+
while x:
66+
y += x % 10
67+
x //= 10
68+
cnt[y] += 1
69+
return max(cnt)
7070
```
7171

7272
### **Java**
7373

7474
```java
7575
class Solution {
7676
public int countBalls(int lowLimit, int highLimit) {
77-
int[] counter = new int[60];
78-
int ans = 0;
77+
int[] cnt = new int[50];
7978
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;
79+
int x = i, y = 0;
80+
for (; x > 0; x /= 10) {
81+
y += x % 10;
8582
}
86-
++counter[s];
87-
ans = Math.max(ans, counter[s]);
83+
++cnt[y];
8884
}
89-
return ans;
85+
return Arrays.stream(cnt).max().getAsInt();
9086
}
9187
}
9288
```
@@ -97,16 +93,14 @@ class Solution {
9793
class Solution {
9894
public:
9995
int countBalls(int lowLimit, int highLimit) {
100-
vector<int> counter(60);
96+
int cnt[50] = {0};
10197
int ans = 0;
10298
for (int i = lowLimit; i <= highLimit; ++i) {
103-
int s = 0, j = i;
104-
while (j) {
105-
s += (j % 10);
106-
j /= 10;
99+
int x = i, y = 0;
100+
for (; x; x /= 10) {
101+
y += x % 10;
107102
}
108-
++counter[s];
109-
ans = max(ans, counter[s]);
103+
ans = max(ans, ++cnt[y]);
110104
}
111105
return ans;
112106
}
@@ -116,21 +110,19 @@ public:
116110
### **Go**
117111
118112
```go
119-
func countBalls(lowLimit int, highLimit int) int {
120-
counter := make([]int, 60)
121-
ans := 0
113+
func countBalls(lowLimit int, highLimit int) (ans int) {
114+
cnt := [50]int{}
122115
for i := lowLimit; i <= highLimit; i++ {
123-
s, j := 0, i
124-
for j > 0 {
125-
s += (j % 10)
126-
j /= 10
116+
x, y := i, 0
117+
for ; x > 0; x /= 10 {
118+
y += x % 10
127119
}
128-
counter[s]++
129-
if counter[s] > ans {
130-
ans = counter[s]
120+
cnt[y]++
121+
if ans < cnt[y] {
122+
ans = cnt[y]
131123
}
132124
}
133-
return ans
125+
return
134126
}
135127
```
136128

solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
class Solution {
22
public:
33
int countBalls(int lowLimit, int highLimit) {
4-
vector<int> counter(60);
4+
int cnt[50] = {0};
55
int ans = 0;
66
for (int i = lowLimit; i <= highLimit; ++i) {
7-
int s = 0, j = i;
8-
while (j) {
9-
s += (j % 10);
10-
j /= 10;
7+
int x = i, y = 0;
8+
for (; x; x /= 10) {
9+
y += x % 10;
1110
}
12-
++counter[s];
13-
ans = max(ans, counter[s]);
11+
ans = max(ans, ++cnt[y]);
1412
}
1513
return ans;
1614
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
func countBalls(lowLimit int, highLimit int) int {
2-
counter := make([]int, 60)
3-
ans := 0
1+
func countBalls(lowLimit int, highLimit int) (ans int) {
2+
cnt := [50]int{}
43
for i := lowLimit; i <= highLimit; i++ {
5-
s, j := 0, i
6-
for j > 0 {
7-
s += (j % 10)
8-
j /= 10
4+
x, y := i, 0
5+
for ; x > 0; x /= 10 {
6+
y += x % 10
97
}
10-
counter[s]++
11-
if counter[s] > ans {
12-
ans = counter[s]
8+
cnt[y]++
9+
if ans < cnt[y] {
10+
ans = cnt[y]
1311
}
1412
}
15-
return ans
13+
return
1614
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
class Solution {
22
public int countBalls(int lowLimit, int highLimit) {
3-
int[] counter = new int[60];
4-
int ans = 0;
3+
int[] cnt = new int[50];
54
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;
5+
int x = i, y = 0;
6+
for (; x > 0; x /= 10) {
7+
y += x % 10;
118
}
12-
++counter[s];
13-
ans = Math.max(ans, counter[s]);
9+
++cnt[y];
1410
}
15-
return ans;
11+
return Arrays.stream(cnt).max().getAsInt();
1612
}
1713
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution:
22
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)
3+
cnt = [0] * 50
4+
for x in range(lowLimit, highLimit + 1):
5+
y = 0
6+
while x:
7+
y += x % 10
8+
x //= 10
9+
cnt[y] += 1
10+
return max(cnt)

0 commit comments

Comments
 (0)