comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
中等 |
|
珂珂喜欢吃香蕉。这里有 n
堆香蕉,第 i
堆中有 piles[i]
根香蕉。警卫已经离开了,将在 h
小时后回来。
珂珂可以决定她吃香蕉的速度 k
(单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 k
根。如果这堆香蕉少于 k
根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉。
珂珂喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。
返回她可以在 h
小时内吃掉所有香蕉的最小速度 k
(k
为整数)。
示例 1:
输入:piles = [3,6,7,11], h = 8 输出:4
示例 2:
输入:piles = [30,11,23,4,20], h = 5 输出:30
示例 3:
输入:piles = [30,11,23,4,20], h = 6 输出:23
提示:
1 <= piles.length <= 104
piles.length <= h <= 109
1 <= piles[i] <= 109
我们注意到,如果珂珂能够以
我们定义二分查找的左边界
时间复杂度 piles
的长度和最大值。空间复杂度
class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
def check(k: int) -> bool:
return sum((x + k - 1) // k for x in piles) <= h
return 1 + bisect_left(range(1, max(piles) + 1), True, key=check)
class Solution {
public int minEatingSpeed(int[] piles, int h) {
int l = 1, r = (int) 1e9;
while (l < r) {
int mid = (l + r) >> 1;
int s = 0;
for (int x : piles) {
s += (x + mid - 1) / mid;
}
if (s <= h) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
}
class Solution {
public:
int minEatingSpeed(vector<int>& piles, int h) {
int l = 1, r = ranges::max(piles);
while (l < r) {
int mid = (l + r) >> 1;
int s = 0;
for (int x : piles) {
s += (x + mid - 1) / mid;
}
if (s <= h) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
};
func minEatingSpeed(piles []int, h int) int {
return 1 + sort.Search(slices.Max(piles), func(k int) bool {
k++
s := 0
for _, x := range piles {
s += (x + k - 1) / k
}
return s <= h
})
}
function minEatingSpeed(piles: number[], h: number): number {
let [l, r] = [1, Math.max(...piles)];
while (l < r) {
const mid = (l + r) >> 1;
const s = piles.map(x => Math.ceil(x / mid)).reduce((a, b) => a + b);
if (s <= h) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
impl Solution {
pub fn min_eating_speed(piles: Vec<i32>, h: i32) -> i32 {
let mut l = 1;
let mut r = *piles.iter().max().unwrap_or(&0);
while l < r {
let mid = (l + r) >> 1;
let mut s = 0;
for x in piles.iter() {
s += (x + mid - 1) / mid;
}
if s <= h {
r = mid;
} else {
l = mid + 1;
}
}
l
}
}
public class Solution {
public int MinEatingSpeed(int[] piles, int h) {
int l = 1, r = (int) 1e9;
while (l < r) {
int mid = (l + r) >> 1;
int s = 0;
foreach (int x in piles) {
s += (x + mid - 1) / mid;
}
if (s <= h) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
}