最大值最小问题
# 875. 爱吃香蕉的珂珂 (opens new window)
class Solution {
public int minEatingSpeed(int[] piles, int h) {
int l = 1;
int r = Arrays.stream(piles).max().getAsInt();
while(l<r){
int mid = (l+r)>>>1;
if(retHour(piles,mid)>h)
l = mid+1;
else
r = mid;
}
return l;
}
int retHour(int[] nums,int v){
int h = 0;
for(int num:nums){
h += num/v;
if(num%v!=0)
h++;
}
return h;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 1011. 在 D 天内送达包裹的能力 (opens new window)
class Solution {
public int shipWithinDays(int[] weights, int days) {
int l = Arrays.stream(weights).max().getAsInt();
int r = Arrays.stream(weights).sum();
while(l<r){
int mid = (l+r)>>>1;
if(retDay(weights,mid)>days)
l = mid+1;
else
r = mid;
}
return l;
}
int retDay(int[] nums,int x){
int days = 0;
for(int i=0;i<nums.length;){
int tmp = x;
while(i<nums.length){
if(tmp<nums[i]){
break;
}else{
tmp -= nums[i];
}
i++;
}
days++;
}
return days;
}
}
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
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
# 2594. 修车的最少时间 (opens new window)
class Solution {
public long repairCars(int[] ranks, int cars) {
long l = 1;
//long r = Arrays.stream(ranks).max().getAsInt()*cars*cars+1;
long r = (long)1e14;
while(l<=r){
long mid = (l+r)>>>1;
if(f(ranks,cars,mid))
r = mid-1;
else
l = mid+1;
}
return l;
}
// time时间内 机械工修车总和数
boolean f(int[] ranks,int cars,long time){
long cnt = 0;
for(int rank:ranks){
cnt += (int)Math.sqrt(time/rank);
}
return cnt>=cars;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23