go: add «1482. Minimum Number of Days to Make m Bouquets»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
8faeaa7e16
commit
d1e6ee86c5
1 changed files with 43 additions and 0 deletions
43
go/minimum-number-of-days-to-make-m-bouquets.go
Normal file
43
go/minimum-number-of-days-to-make-m-bouquets.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue