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 * 104
flowers[i].length == 2
1 <= starti <= endi <= 109
1 <= people.length <= 5 * 104
1 <= people[i] <= 109
When 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:
The brute force approach to finding the number of blooming flowers for each person's arrival time involves checking each person individually. For each person, we will examine all the flower blooming periods to see if the person arrived during the bloom.
Here's how the algorithm would work step-by-step:
def number_of_flowers_in_full_bloom_brute_force(flowers, people):
results = []
for person_arrival_time in people:
number_of_flowers_blooming = 0
# Iterate through each flower's bloom period.
for flower in flowers:
bloom_start = flower[0]
bloom_end = flower[1]
# Check if the person arrived during the flower's bloom.
if bloom_start <= person_arrival_time <= bloom_end:
number_of_flowers_blooming += 1
results.append(number_of_flowers_blooming)
return results
We want to efficiently count blooming flowers for different people's arrival times. The key is to organize the flower blooming and ending times so we can quickly find how many flowers are blooming when each person arrives using a binary search-like technique.
Here's how the algorithm would work step-by-step:
def number_of_flowers_in_full_bloom(flowers, people):
start_times = [flower[0] for flower in flowers]
end_times = [flower[1] for flower in flowers]
start_times.sort()
end_times.sort()
result = []
for person_arrival_time in people:
# Find num flowers blooming when the person arrives.
started_blooming = find_index(start_times, person_arrival_time + 1)
# Find num flowers that already stopped blooming.
stopped_blooming = find_index(end_times, person_arrival_time + 1)
result.append(started_blooming - stopped_blooming)
return result
def find_index(flower_times, arrival_time):
low = 0
high = len(flower_times)
while low < high:
mid = (low + high) // 2
if flower_times[mid] < arrival_time:
low = mid + 1
else:
high = mid
return low
Case | How to Handle |
---|---|
flowers is null or empty | Return an array of zeros with the same length as the people array. |
people is null or empty | Return an empty array. |
flowers array contains overlapping bloom ranges | The algorithm should correctly count flowers in bloom even if their ranges overlap. |
A person arrives exactly at the start or end time of a flower's bloom | The problem statement clarifies inclusive ranges so the solution should include these times. |
All flowers bloom at the exact same time | The algorithm should correctly count all blooming flowers for those arrival times. |
The number of flowers or people is very large | Use an efficient algorithm like binary search to avoid exceeding time limits. |
Flowers have very large bloom ranges, potentially leading to integer overflow if calculating durations | Avoid explicit duration calculations and rely on comparisons directly within the input range values. |
People arrive at times outside any flower's bloom range | The corresponding entry in the answer array should be 0. |