Intuition
A brute force approach to this is to test every possible day to build m
bouquets and track the lowest one.
But we can optimize this process by iterating over the possible days and binary searching through it. This way we would test every possible day to build m
bouquets, and if we are able to build them, then we reduce our search space into the left side. Meaning, we are looking if we can build the bouquets in any less days.
Similarly, if we are unable to build the bouquets that means we are underplaying the days and need to increase it - so look in the right search space.
To understand this better, read more about Binary Search
More problems like this
1011 Capacity To Ship Packages Within D Days
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since we are reducing our search space of size K in half (days) but for every day that we test - we also iterate the entire array (flowers) of size N.
-
Memory
The memory usage is since we are not using any extra data structure.
— A