Skip to content

Commit 41ca792

Browse files
committed
feat: add solutions to lc problem: No.1760
No.1760.Minimum Limit of Balls in a Bag
1 parent 79125f4 commit 41ca792

File tree

8 files changed

+191
-75
lines changed

8 files changed

+191
-75
lines changed

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README.md

+66-25
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@
8383
```python
8484
class Solution:
8585
def minimumSize(self, nums: List[int], maxOperations: int) -> int:
86-
def f(x):
87-
return sum((v - 1) // x for v in nums) <= maxOperations
86+
def check(mx: int) -> bool:
87+
return sum((x - 1) // mx for x in nums) <= maxOperations
8888

89-
return bisect_left(range(1, max(nums) + 1), True, key=f) + 1
89+
return bisect_left(range(1, max(nums)), True, key=check) + 1
9090
```
9191

9292
### **Java**
@@ -96,14 +96,17 @@ class Solution:
9696
```java
9797
class Solution {
9898
public int minimumSize(int[] nums, int maxOperations) {
99-
int left = 1, right = (int) 1e9;
99+
int left = 1, right = 0;
100+
for (int x : nums) {
101+
right = Math.max(right, x);
102+
}
100103
while (left < right) {
101-
int mid = (left + right) >>> 1;
102-
long s = 0;
103-
for (int v : nums) {
104-
s += (v - 1) / mid;
104+
int mid = (left + right) >> 1;
105+
long cnt = 0;
106+
for (int x : nums) {
107+
cnt += (x - 1) / mid;
105108
}
106-
if (s <= maxOperations) {
109+
if (cnt <= maxOperations) {
107110
right = mid;
108111
} else {
109112
left = mid + 1;
@@ -122,11 +125,16 @@ public:
122125
int minimumSize(vector<int>& nums, int maxOperations) {
123126
int left = 1, right = *max_element(nums.begin(), nums.end());
124127
while (left < right) {
125-
int mid = left + right >> 1;
126-
long s = 0;
127-
for (int v : nums) s += (v - 1) / mid;
128-
if (s <= maxOperations) right = mid;
129-
else left = mid + 1;
128+
int mid = (left + right) >> 1;
129+
long long cnt = 0;
130+
for (int x : nums) {
131+
cnt += (x - 1) / mid;
132+
}
133+
if (cnt <= maxOperations) {
134+
right = mid;
135+
} else {
136+
left = mid + 1;
137+
}
130138
}
131139
return left;
132140
}
@@ -137,15 +145,26 @@ public:
137145
138146
```go
139147
func minimumSize(nums []int, maxOperations int) int {
140-
return 1 + sort.Search(1e9, func(x int) bool {
141-
x++
142-
s := 0
143-
for _, v := range nums {
144-
s += (v - 1) / x
148+
r := 0
149+
for _, x := range nums {
150+
r = max(r, x)
151+
}
152+
return 1 + sort.Search(r, func(mx int) bool {
153+
mx++
154+
cnt := 0
155+
for _, x := range nums {
156+
cnt += (x - 1) / mx
145157
}
146-
return s <= maxOperations
158+
return cnt <= maxOperations
147159
})
148160
}
161+
162+
func max(a, b int) int {
163+
if a > b {
164+
return a
165+
}
166+
return b
167+
}
149168
```
150169

151170
### **JavaScript**
@@ -158,14 +177,14 @@ func minimumSize(nums []int, maxOperations int) int {
158177
*/
159178
var minimumSize = function (nums, maxOperations) {
160179
let left = 1;
161-
let right = 1e9;
180+
let right = Math.max(...nums);
162181
while (left < right) {
163182
const mid = (left + right) >> 1;
164-
let s = 0;
165-
for (const v of nums) {
166-
s += Math.floor((v - 1) / mid);
183+
let cnt = 0;
184+
for (const x of nums) {
185+
cnt += ~~((x - 1) / mid);
167186
}
168-
if (s <= maxOperations) {
187+
if (cnt <= maxOperations) {
169188
right = mid;
170189
} else {
171190
left = mid + 1;
@@ -175,6 +194,28 @@ var minimumSize = function (nums, maxOperations) {
175194
};
176195
```
177196

197+
### **TypeScript**
198+
199+
```ts
200+
function minimumSize(nums: number[], maxOperations: number): number {
201+
let left = 1;
202+
let right = Math.max(...nums);
203+
while (left < right) {
204+
const mid = (left + right) >> 1;
205+
let cnt = 0;
206+
for (const x of nums) {
207+
cnt += ~~((x - 1) / mid);
208+
}
209+
if (cnt <= maxOperations) {
210+
right = mid;
211+
} else {
212+
left = mid + 1;
213+
}
214+
}
215+
return left;
216+
}
217+
```
218+
178219
### **...**
179220

180221
```

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README_EN.md

+66-25
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,28 @@ Binary search.
6464
```python
6565
class Solution:
6666
def minimumSize(self, nums: List[int], maxOperations: int) -> int:
67-
def f(x):
68-
return sum((v - 1) // x for v in nums) <= maxOperations
67+
def check(mx: int) -> bool:
68+
return sum((x - 1) // mx for x in nums) <= maxOperations
6969

70-
return bisect_left(range(1, max(nums) + 1), True, key=f) + 1
70+
return bisect_left(range(1, max(nums)), True, key=check) + 1
7171
```
7272

7373
### **Java**
7474

7575
```java
7676
class Solution {
7777
public int minimumSize(int[] nums, int maxOperations) {
78-
int left = 1, right = (int) 1e9;
78+
int left = 1, right = 0;
79+
for (int x : nums) {
80+
right = Math.max(right, x);
81+
}
7982
while (left < right) {
80-
int mid = (left + right) >>> 1;
81-
long s = 0;
82-
for (int v : nums) {
83-
s += (v - 1) / mid;
83+
int mid = (left + right) >> 1;
84+
long cnt = 0;
85+
for (int x : nums) {
86+
cnt += (x - 1) / mid;
8487
}
85-
if (s <= maxOperations) {
88+
if (cnt <= maxOperations) {
8689
right = mid;
8790
} else {
8891
left = mid + 1;
@@ -101,11 +104,16 @@ public:
101104
int minimumSize(vector<int>& nums, int maxOperations) {
102105
int left = 1, right = *max_element(nums.begin(), nums.end());
103106
while (left < right) {
104-
int mid = left + right >> 1;
105-
long s = 0;
106-
for (int v : nums) s += (v - 1) / mid;
107-
if (s <= maxOperations) right = mid;
108-
else left = mid + 1;
107+
int mid = (left + right) >> 1;
108+
long long cnt = 0;
109+
for (int x : nums) {
110+
cnt += (x - 1) / mid;
111+
}
112+
if (cnt <= maxOperations) {
113+
right = mid;
114+
} else {
115+
left = mid + 1;
116+
}
109117
}
110118
return left;
111119
}
@@ -116,15 +124,26 @@ public:
116124
117125
```go
118126
func minimumSize(nums []int, maxOperations int) int {
119-
return 1 + sort.Search(1e9, func(x int) bool {
120-
x++
121-
s := 0
122-
for _, v := range nums {
123-
s += (v - 1) / x
127+
r := 0
128+
for _, x := range nums {
129+
r = max(r, x)
130+
}
131+
return 1 + sort.Search(r, func(mx int) bool {
132+
mx++
133+
cnt := 0
134+
for _, x := range nums {
135+
cnt += (x - 1) / mx
124136
}
125-
return s <= maxOperations
137+
return cnt <= maxOperations
126138
})
127139
}
140+
141+
func max(a, b int) int {
142+
if a > b {
143+
return a
144+
}
145+
return b
146+
}
128147
```
129148

130149
### **JavaScript**
@@ -137,14 +156,14 @@ func minimumSize(nums []int, maxOperations int) int {
137156
*/
138157
var minimumSize = function (nums, maxOperations) {
139158
let left = 1;
140-
let right = 1e9;
159+
let right = Math.max(...nums);
141160
while (left < right) {
142161
const mid = (left + right) >> 1;
143-
let s = 0;
144-
for (const v of nums) {
145-
s += Math.floor((v - 1) / mid);
162+
let cnt = 0;
163+
for (const x of nums) {
164+
cnt += ~~((x - 1) / mid);
146165
}
147-
if (s <= maxOperations) {
166+
if (cnt <= maxOperations) {
148167
right = mid;
149168
} else {
150169
left = mid + 1;
@@ -154,6 +173,28 @@ var minimumSize = function (nums, maxOperations) {
154173
};
155174
```
156175

176+
### **TypeScript**
177+
178+
```ts
179+
function minimumSize(nums: number[], maxOperations: number): number {
180+
let left = 1;
181+
let right = Math.max(...nums);
182+
while (left < right) {
183+
const mid = (left + right) >> 1;
184+
let cnt = 0;
185+
for (const x of nums) {
186+
cnt += ~~((x - 1) / mid);
187+
}
188+
if (cnt <= maxOperations) {
189+
right = mid;
190+
} else {
191+
left = mid + 1;
192+
}
193+
}
194+
return left;
195+
}
196+
```
197+
157198
### **...**
158199

159200
```

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/Solution.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ class Solution {
33
int minimumSize(vector<int>& nums, int maxOperations) {
44
int left = 1, right = *max_element(nums.begin(), nums.end());
55
while (left < right) {
6-
int mid = left + right >> 1;
7-
long s = 0;
8-
for (int v : nums) s += (v - 1) / mid;
9-
if (s <= maxOperations)
6+
int mid = (left + right) >> 1;
7+
long long cnt = 0;
8+
for (int x : nums) {
9+
cnt += (x - 1) / mid;
10+
}
11+
if (cnt <= maxOperations) {
1012
right = mid;
11-
else
13+
} else {
1214
left = mid + 1;
15+
}
1316
}
1417
return left;
1518
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
func minimumSize(nums []int, maxOperations int) int {
2-
return 1 + sort.Search(1e9, func(x int) bool {
3-
x++
4-
s := 0
5-
for _, v := range nums {
6-
s += (v - 1) / x
2+
r := 0
3+
for _, x := range nums {
4+
r = max(r, x)
5+
}
6+
return 1 + sort.Search(r, func(mx int) bool {
7+
mx++
8+
cnt := 0
9+
for _, x := range nums {
10+
cnt += (x - 1) / mx
711
}
8-
return s <= maxOperations
12+
return cnt <= maxOperations
913
})
14+
}
15+
16+
func max(a, b int) int {
17+
if a > b {
18+
return a
19+
}
20+
return b
1021
}

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/Solution.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
class Solution {
22
public int minimumSize(int[] nums, int maxOperations) {
3-
int left = 1, right = (int) 1e9;
3+
int left = 1, right = 0;
4+
for (int x : nums) {
5+
right = Math.max(right, x);
6+
}
47
while (left < right) {
5-
int mid = (left + right) >>> 1;
6-
long s = 0;
7-
for (int v : nums) {
8-
s += (v - 1) / mid;
8+
int mid = (left + right) >> 1;
9+
long cnt = 0;
10+
for (int x : nums) {
11+
cnt += (x - 1) / mid;
912
}
10-
if (s <= maxOperations) {
13+
if (cnt <= maxOperations) {
1114
right = mid;
1215
} else {
1316
left = mid + 1;

solution/1700-1799/1760.Minimum Limit of Balls in a Bag/Solution.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
*/
66
var minimumSize = function (nums, maxOperations) {
77
let left = 1;
8-
let right = 1e9;
8+
let right = Math.max(...nums);
99
while (left < right) {
1010
const mid = (left + right) >> 1;
11-
let s = 0;
12-
for (const v of nums) {
13-
s += Math.floor((v - 1) / mid);
11+
let cnt = 0;
12+
for (const x of nums) {
13+
cnt += ~~((x - 1) / mid);
1414
}
15-
if (s <= maxOperations) {
15+
if (cnt <= maxOperations) {
1616
right = mid;
1717
} else {
1818
left = mid + 1;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def minimumSize(self, nums: List[int], maxOperations: int) -> int:
3-
def f(x):
4-
return sum((v - 1) // x for v in nums) <= maxOperations
3+
def check(mx: int) -> bool:
4+
return sum((x - 1) // mx for x in nums) <= maxOperations
55

6-
return bisect_left(range(1, max(nums) + 1), True, key=f) + 1
6+
return bisect_left(range(1, max(nums)), True, key=check) + 1

0 commit comments

Comments
 (0)