lc-go/solutions/8/q875/solution.go

32 lines
533 B
Go

// Package q875 implements a solution for https://leetcode.com/problems/koko-eating-bananas/
package q875
import (
"math"
"slices"
)
func canEat(piles []int, h, speed int) bool {
needed := 0
for _, n := range piles {
needed += int(math.Ceil(float64(n) / float64(speed)))
}
return needed <= h
}
func minEatingSpeed(piles []int, h int) int {
maxK := slices.Max(piles)
l, r := 1, maxK+1
for l < r {
m := (l + r) / 2
if canEat(piles, h, m) {
r = m
} else {
l = m + 1
}
}
return l
}
var _ = minEatingSpeed