add new solutions
This commit is contained in:
parent
f249852923
commit
f297e11859
10 changed files with 373 additions and 0 deletions
37
solutions/32/q3296/solution.go
Normal file
37
solutions/32/q3296/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue