Number of Flowers in Full Bloom - Problem
Imagine you're organizing a beautiful flower garden tour! ๐ธ You have a garden where different flowers bloom at different times throughout the year, and visitors arrive at various times to see the spectacle.
You're given a
Your mission: For each visitor, determine exactly how many flowers they'll see in full bloom when they arrive! Return an array where
Example: If flowers bloom during periods [[1,6],[3,7],[9,12],[4,13]] and people arrive on days [2,3,7,11], then visitors will see [1,2,2,2] flowers respectively.
You're given a
flowers array where flowers[i] = [start_i, end_i] represents the i-th flower's blooming period from day start_i to day end_i (both days inclusive). You're also given a people array where people[i] is the day when the i-th person arrives to visit your garden.Your mission: For each visitor, determine exactly how many flowers they'll see in full bloom when they arrive! Return an array where
answer[i] is the number of flowers blooming when person i visits.Example: If flowers bloom during periods [[1,6],[3,7],[9,12],[4,13]] and people arrive on days [2,3,7,11], then visitors will see [1,2,2,2] flowers respectively.
Input & Output
example_1.py โ Basic Garden Tour
$
Input:
flowers = [[1,6],[3,7],[9,12],[4,13]]
people = [2,3,7,11]
โบ
Output:
[1,2,2,2]
๐ก Note:
Person at day 2: Only flower [1,6] is blooming (1 flower). Person at day 3: Flowers [1,6] and [3,7] are blooming (2 flowers). Person at day 7: Flowers [3,7] and [4,13] are blooming (2 flowers). Person at day 11: Flowers [9,12] and [4,13] are blooming (2 flowers).
example_2.py โ Single Flower
$
Input:
flowers = [[1,10]]
people = [3,3,2]
โบ
Output:
[1,1,1]
๐ก Note:
There's only one flower blooming from day 1 to 10. All three people arrive during this period, so each person sees exactly 1 flower in bloom.
example_3.py โ No Overlapping Blooms
$
Input:
flowers = [[1,3],[4,6],[7,9]]
people = [2,5,8,10]
โบ
Output:
[1,1,1,0]
๐ก Note:
Each person arrives during different non-overlapping bloom periods. The last person arrives after all flowers have finished blooming, so they see 0 flowers.
Visualization
Tap to expand
Understanding the Visualization
1
Timeline Setup
Create a timeline showing all flower blooming periods
2
Event Extraction
Extract start and end events from flower intervals
3
Binary Search Magic
Use binary search to instantly count active blooms for any query time
4
Efficient Queries
Answer all visitor queries in O(log n) time each
Key Takeaway
๐ฏ Key Insight: By converting flower intervals into sorted start/end events and using binary search, we can answer bloom count queries in O(log n) time instead of checking every flower individually.
Time & Space Complexity
Time Complexity
O(n*m)
For each of n people, we check all m flowers, resulting in n*m operations
โ Linear Growth
Space Complexity
O(1)
Only using constant extra space for counters and variables
โ Linear Space
Constraints
- 1 โค flowers.length โค 5 ร 104
- flowers[i].length == 2
- 1 โค starti โค endi โค 109
- 1 โค people.length โค 5 ร 104
- 1 โค people[i] โค 109
- Time limit: 2 seconds
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code