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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package 03.mca_01;
// leetcode 875题
// 一定有答案
// 管理员1小时5堆-1
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);
}
// 1......Max
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;
}
// piles : 每一堆香蕉的个数都在piles里
// speed : 猩猩的速度
// 在这个速度下,几小时吃完!
public static long hours(int[] piles, int speed) {
long ans = 0;
int offset = speed - 1;
for (int pile : piles) {
ans += (pile + offset) / speed;
}
return ans;
}
}