go: add «1482. Minimum Number of Days to Make m Bouquets»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-06-19 10:38:59 +02:00
parent 8faeaa7e16
commit d1e6ee86c5
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,43 @@
package main
func minDays(bloomDay []int, m int, k int) int {
getBouquets := func(day int) int {
bouquets := 0
count := 0
for i := range bloomDay {
if bloomDay[i] <= day {
count++
} else {
count = 0
}
if count == k {
bouquets++
count = 0
}
}
return bouquets
}
firstDay := 0
lastDay := 0
for _, day := range bloomDay {
lastDay = max(lastDay, day)
}
minDay := -1
for firstDay <= lastDay {
mid := (firstDay + lastDay) / 2
if getBouquets(mid) >= m {
minDay = mid
lastDay = mid - 1
} else {
firstDay = mid + 1
}
}
return minDay
}