lc-go/solutions/8/q875/solution.go
2026-01-25 11:56:12 +09:00

31 lines
440 B
Go

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