lc-go/solutions/32/q3296/solution.go
2026-03-13 16:18:54 +09:00

37 lines
763 B
Go

// Package q3296 implements a solution for https://leetcode.com/problems/minimum-number-of-seconds-to-make-mountain-height-zero/
package q3296
import (
"math"
"slices"
)
func canFinishIn(mHeight int, wTimes []int, secs int) bool {
decHeight := 0
for _, t := range wTimes {
h := int(math.Sqrt(float64(secs) * 2 / float64(t)))
if t*h*(h+1)/2 > secs {
h--
}
decHeight += h
}
return decHeight >= mHeight
}
func minNumberOfSeconds(mountainHeight int, workerTimes []int) int64 {
worse := slices.Max(workerTimes) * (mountainHeight + 1) * mountainHeight / 2
l, r := 0, worse+1
for l < r {
m := (l + r) / 2
if canFinishIn(mountainHeight, workerTimes, m) {
r = m
} else {
l = m + 1
}
}
return int64(l)
}
var _ = minNumberOfSeconds