2251. Number of Flowers in Full Bloom
Asked at Databricks
Difficulty
Hard
Time
O((n + m) log n)
Space
O(n)
Pattern
Array
Companies
1
Problem
You get a list of flowers. Each flower is described by two days: flowers[i] = [start, end]. That flower is in full bloom on every day from start to end, including both of those days.
You also get a list people. Each person arrives on one specific day, people[i]. For every person, count how many flowers are blooming on the day they arrive.
Return an array answer of the same length as people, where answer[i] is the number of blooming flowers on the arrival day of person i. The days can be very large (up to 10^9), so you cannot loop over every day.
Which flowers are open on each visit day
Constraints
- ●
1 <= flowers.length <= 5 * 10^4 - ●
flowers[i].length == 2 - ●
1 <= start_i <= end_i <= 10^9 - ●
1 <= people.length <= 5 * 10^4 - ●
1 <= people[i] <= 10^9
Examples
flowers = [[1,6],[3,7],[9,12],[4,13]], people = [2,3,7,11][1,2,2,2]flowers = [[1,10],[3,3]], people = [3,3,2][2,2,1]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Databricks | Hard | View all Databricks questions → |
How to Think About It
Brute force: for each person, iterate all flowers and count how many are blooming — O(n * m) time.
Sort the bloom start days and bloom end days separately.
For a given day d, the number of blooming flowers = (flowers with start <= d) - (flowers with end < d).
Use binary search on the sorted start and end arrays to compute each count in O(log n).
Optimal: sort starts, sort ends, for each person binary search both arrays — O((n + m) log n) time.
Optimal Approach
Separate the bloom start days and bloom end days into two sorted arrays. For each person arriving on day d, use binary search to find the number of flowers that have started blooming (start <= d) and subtract the number that have already finished blooming (end < d). The result is the count of flowers in full bloom on day d. This gives O((n + m) log n) time where n is the number of flowers and m is the number of people.
Started minus ended
What Trips People Up in Real Interviews
Clarify whether the bloom interval is inclusive on both ends — it usually is.
Ask if people can arrive on the same day — yes, and each person's count is independent.
Use bisect_right for end days (flowers with end < d) and bisect_right for start days (flowers with start <= d).
Edge case: a flower that blooms on exactly one day (start == end) should be counted for that day.
Discuss the tradeoff between the sorting + binary search approach vs. a sweep line with a prefix sum.
Solution Code
O((n + m) log n) timeimport bisect
def fullBloomFlowers(flowers, people):
starts = sorted(f[0] for f in flowers)
ends = sorted(f[1] for f in flowers)
result = []
for p in people:
started = bisect.bisect_right(starts, p)
ended = bisect.bisect_left(ends, p)
result.append(started - ended)
return resultFrequently Asked Questions
What is the Number of Flowers in Full Bloom problem?
You are given a list of flowers, each with a bloom start day and bloom end day. A flower is in full bloom on any day between its start and end (inclusive). Given a list of people who each visit on a specific day, determine how many flowers are in full bloom on each person's visit day.
How do you solve Number of Flowers in Full Bloom?
The optimal approach runs in O((n + m) log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Number of Flowers in Full Bloom?
Number of Flowers in Full Bloom is asked at Databricks. It is a hard difficulty problem.
What are common mistakes on Number of Flowers in Full Bloom?
- Clarify whether the bloom interval is inclusive on both ends — it usually is.
- Ask if people can arrive on the same day — yes, and each person's count is independent.
- Use bisect_right for end days (flowers with end < d) and bisect_right for start days (flowers with start <= d).
- Edge case: a flower that blooms on exactly one day (start == end) should be counted for that day.
- Discuss the tradeoff between the sorting + binary search approach vs. a sweep line with a prefix sum.