package main
import (
"fmt"
)
func canEatAll(piles []int, mid int, H int) bool {
var sum int = 0
for i := 0; i < len(piles); i++ {
sum = sum + (piles[i] - 1)/mid + 1
}
return sum <= H //sum小于H,则表示在守卫回来之前,已经吃完
}
func getArrayMaxium(arr []int) int {
maximum := arr[0]
for _, value := range arr {
if maximum < value {
maximum = value
}
}
return maximum
}
func canEatMinimum(piles []int, H int) int {
lo := 1
hi := getArrayMaxium(piles[:])
fmt.Println("hi is : ", hi)
for lo < hi {
mid := (lo + hi)/2
if canEatAll(piles[:], mid, H) {
hi = mid
} else {
lo = mid + 1
}
}
return lo
}
func main() {
piles := [...]int{30, 11, 23, 4, 20}
H := 5
minimum := canEatMinimum(piles[:], H)
fmt.Println("can eat minimum : ", minimum)
}
Go语言:爱吃香蕉的珂珂
最新推荐文章于 2022-05-15 22:18:51 发布