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 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.

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

Visualization

Tap to expand
Smart Garden Timeline SystemDays: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15๐ŸŒธ Flower 1 [1-6]๐ŸŒบ Flower 2 [3-7]๐ŸŒป Flower 3 [9-12]๐ŸŒท Flower 4 [4-13]Binary Search Arrays:Start Events: [1, 3, 4, 9] (sorted)End Events: [6, 7, 12, 13] (sorted)Query: Person arrives at day 7โ€ข Binary search starts โ‰ค 7: found 3 flowers (positions 0,1,2)๐Ÿ‘คPerson at day 7Sees: 2 flowersCalculation for day 7:Started by day 7: 3 flowersEnded before day 7: 1 flowerActive: 3 - 1 = 2 flowers๐ŸŽฏ Key Insight: Binary search makes each query O(log n) instead of O(n)!
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.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
58.0K Views
High Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen