5
listOfFlowerBloomDays: List[int],6
targetNumberOfBouquets: int,7
flowersPerBouquet: int,10
def numberOfBouquetsWeCanMakeOnThisDay(dayThatWeAreChecking):12
currentListOfAdjacentBloomedFlowers = []13
numberOfBouquetsWeCanMakeOnThisDay = 015
for dayThatFlowerBlooms in listOfFlowerBloomDays:17
# check if the flower has bloomed on this day18
if dayThatFlowerBlooms <= dayThatWeAreChecking:20
# add to the list an adjacent bloomed flowers, I use 'x' because the description uses an 'x'21
currentListOfAdjacentBloomedFlowers.append("x")24
# we've hit a day where we don't have a bloomed flower, so the list of adjacent bloomed flowers has to be reset25
# BUT FIRST figure out how many bouquets we can make with this list of adjacent bloomed flowers26
numberOfBouquetsWeCanMakeOnThisDay += (27
len(currentListOfAdjacentBloomedFlowers) // flowersPerBouquet30
# RESET list of adjacent bloomed flowers cause we're on a day where the a flower has not bloomed yet31
currentListOfAdjacentBloomedFlowers = []33
# we've gone through the entire listOfFlowerBloomDays list and need to check if the "residual" current list34
# of adjacent bloomed flowers can make a bouquet ... so handle it here35
numberOfBouquetsWeCanMakeOnThisDay += (36
len(currentListOfAdjacentBloomedFlowers) // flowersPerBouquet39
return numberOfBouquetsWeCanMakeOnThisDay41
# if the TOTAL amount of flowers we need doesn't match the number of possible flowers we can grow,42
# then the given inputs are impossible for making enough bouquets (we don't have enough flowers)43
totalNumberOfFlowersNeeded = targetNumberOfBouquets * flowersPerBouquet44
numberOfFlowersWeCanGrow = len(listOfFlowerBloomDays)45
if numberOfFlowersWeCanGrow < totalNumberOfFlowersNeeded:48
# no need to go past the day of the flower with the longest bloom date50
rightDay = max(listOfFlowerBloomDays)52
while leftDay < rightDay:54
# currentDay is functioning as the "mid" of a binary search55
currentDay = leftDay + (rightDay - leftDay) // 257
# as in most binary searches, we check if the mid (which I'm calling 'currentDay') satisfies the constraint58
# that is, if we can make the target amount of bouquets on this day59
if numberOfBouquetsWeCanMakeOnThisDay(currentDay) < targetNumberOfBouquets:61
# womp womp, we can't make enough bouquets on this day, so set up for next iteration62
# the "correct day" is on the right side, so we get rid of all the "incorrect days" on the left side63
# by updating the left to the currentDay+164
leftDay = currentDay + 167
# yay, we can make enough bouquets on this day, but we don't know if this is the "minimum day"68
# we discard the right side to keep searching71
# leftDay >= rightDay, so we've found the "minimum day"