1760. 袋子里最少数目的球

二分查找递归寻找答案是否满足条件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public int minimumSize(int[] nums, int maxOperations) {
int l = 1;
int r = Integer.MAX_VALUE;
int ans = 0;
while (l <= r) {
int mid = l + (r-l)/2;
if (check(nums, maxOperations, mid)){
r = mid -1;
ans = mid;
}else{
l = mid + 1;
}
}
return ans;
}

public boolean check(int[] nums, int maxOperations, int mid){
int count = 0;
for (int num : nums) {
if (num % mid == 0){
count += num/mid -1;
}else {
count += num / mid;
}
}

return count <= maxOperations;
}
}