You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
788 B

2 years ago
package 03.mca_01;
// leetcode 875题
2 years ago
// 一定有答案
// 管理员1小时5堆-1
2 years ago
public class Code05_KokoEatingBananas {
public static int minEatingSpeed(int[] piles, int h) {
int L = 1;
int R = 0;
for (int pile : piles) {
R = Math.max(R, pile);
}
2 years ago
// 1......Max
2 years ago
int ans = 0;
int M = 0;
while (L <= R) {
M = L + ((R - L) >> 1);
if (hours(piles, M) <= h) {
ans = M;
R = M - 1;
} else {
L = M + 1;
}
}
return ans;
}
2 years ago
// piles : 每一堆香蕉的个数都在piles里
// speed : 猩猩的速度
// 在这个速度下,几小时吃完!
2 years ago
public static long hours(int[] piles, int speed) {
long ans = 0;
2 years ago
int offset = speed - 1;
for (int pile : piles) {
ans += (pile + offset) / speed;
}
return ans;
}
}