You are given a 0-indexed 2D integer array flowers, where flowers[i] = [starti, endi] means the ith flower will be in full bloom from starti to endi (inclusive). You are also given a 0-indexed integer array people of size n, where people[i] is the time that the ith person will arrive to see the flowers.
Return an integer array answer of size n, where answer[i] is the number of flowers that are in full bloom when the ith person arrives.
Example 1:
Input: flowers = [[1,6],[3,7],[9,12],[4,13]], people = [2,3,7,11] Output: [1,2,2,2] Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive. For each person, we return the number of flowers in full bloom during their arrival.
Example 2:
Input: flowers = [[1,10],[3,3]], people = [3,3,2] Output: [2,2,1] Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive. For each person, we return the number of flowers in full bloom during their arrival.
Constraints:
1 <= flowers.length <= 5 * 104flowers[i].length == 21 <= starti <= endi <= 1091 <= people.length <= 5 * 1041 <= people[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you need to figure out how many flowers are blooming at specific moments in time by checking every single flower for every single moment. This brute force approach tests every possibility from scratch without taking any shortcuts.
Here's how the algorithm would work step-by-step:
def full_bloom_flowers(flowers, people): # We store the final count for each queried arrival time to return at the end bloom_count_results = [] # Iterate through every arrival time to evaluate flower coverage individually for person_time in people: current_flower_count = 0 for flower_interval in flowers: start_bloom = flower_interval[0] end_bloom = flower_interval[1] if start_bloom <= person_time and person_time <= end_bloom: current_flower_count += 1 bloom_count_results.append(current_flower_count) # Return the accumulated counts matching the exact order of the people queries return bloom_count_resultsInstead of checking every single flower's schedule one by one every time someone visits, the optimal approach organizes all the starting and ending times in order. By sorting these times, we can use a smart counting trick to quickly figure out how many flowers are blooming at any given moment.
Here's how the algorithm would work step-by-step:
import bisect
def full_bloom_flowers(flower_intervals, arrival_times):
start_times = sorted([flower[0] for flower in flower_intervals])
end_times = sorted([flower[1] for flower in flower_intervals])
# Sorting allows binary search for fast lookups instead of checking every flower.
bloom_results = []
for arrival_time in arrival_times:
started_count = bisect.bisect_right(start_times, arrival_time)
# Find how many flowers started before or at this arrival time.
finished_count = bisect.bisect_left(end_times, arrival_time)
# Subtract finished from started to isolate the currently blooming flowers.
active_flowers = started_count - finished_count
bloom_results.append(active_flowers)
return bloom_results| Case | How to Handle |
|---|---|
| Person arrival time falls completely outside the bloom range of all flowers | Binary search returns zero for both start and end counts, resulting in a difference of zero. |
| Person arrives exactly on the start day or end day of a flower bloom | Using upper_bound for start times and lower_bound for end times correctly includes boundary days since blooms are inclusive. |
| All flowers have the exact same bloom and fade times | Sorting both start and end arrays correctly groups the identical values, allowing binary search to count them all accurately. |
| Extremely large constraints where flower times and arrival times reach up to 10 to the 9th power | Using binary search on sorted arrays ensures an efficient logarithmic time complexity per query instead of a slow linear scan. |
| People arrive in completely unsorted order | Storing original indices alongside arrival times allows us to sort the queries for processing and then restore the original order in the result array. |
| Multiple flowers start or end blooming at the exact same time | Sorting the start and end time arrays allows binary search to correctly determine the exact number of active flowers regardless of duplicates. |
| The arrival times of people are completely outside the range of all flower bloom times, either all too early or all too late | Binary search bounds will evaluate to zero active flowers, correctly outputting a list of zeros. |
| Maximum possible inputs causing potential integer overflow in languages with strict sizing | Standard 32-bit signed integers are sufficient since constraints fit within standard limits, but using 64-bit integers prevents any overflow risks during sorting and indexing. |