Number of Flowers in Full Bloom - Complete Solution Guide
Number of Flowers in Full Bloom is LeetCode problem 2251, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 0-indexed 2D integer array flowers , where flowers[i] = [start i , end i ] means the i th flower will be in full bloom from start i to end i ( inclusive ). You are also given a 0-indexed integer array people of size n , where people[i] is the time that the i th 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 i th person arrives. Example 1: Input: flowers = [[1,6],[3,7],[9,12
Detailed Explanation
The problem asks us to determine, for each person arriving at a specific time, how many flowers are in full bloom. We are given a list of flowers where each flower has a start and end time indicating when it's in full bloom (inclusive). We are also given a list of people and their arrival times. The goal is to return a list where each element is the number of flowers in bloom at the corresponding person's arrival time.
Solution Approach
The solution involves first extracting the start and end times of the flowers into separate arrays. These arrays are then sorted to allow for binary searches. For each person, we perform two binary searches: one on the sorted start times to find the number of flowers that have started blooming by the person's arrival time, and another on the sorted end times to find the number of flowers that have ended blooming by the person's arrival time. The difference between these two counts gives us the number of flowers in bloom at that person's arrival time. This process is repeated for each person, and the results are stored in an answer array.
Step-by-Step Algorithm
- Step 1: Extract the start times and end times from the 'flowers' array into two separate arrays, 'starts' and 'ends'.
- Step 2: Sort both the 'starts' and 'ends' arrays in ascending order.
- Step 3: Initialize an empty 'ans' array to store the results for each person.
- Step 4: Iterate through the 'people' array. For each person's arrival time 'p':
- Step 5: Perform a binary search on the 'starts' array to find the number of flowers that have started blooming before or at time 'p'. This is equivalent to finding the index of the first element greater than 'p' (bisect_right or upper_bound).
- Step 6: Perform a binary search on the 'ends' array to find the number of flowers that have ended blooming before time 'p'. This is equivalent to finding the index of the first element not less than 'p' (bisect_left or lower_bound).
- Step 7: Calculate the difference between the results of the two binary searches (started - ended), which represents the number of flowers in bloom at time 'p'.
- Step 8: Append this difference to the 'ans' array.
- Step 9: After iterating through all the people, return the 'ans' array.
Key Insights
- Insight 1: The brute-force approach of iterating through flowers for each person to check if the arrival time falls within the bloom period would be too slow (O(n*m) where n is the number of flowers and m is the number of people).
- Insight 2: Binary search can efficiently find the number of flowers that have started blooming before or at a given time and the number of flowers that have ended blooming before a given time.
- Insight 3: Sorting the start and end times of the flowers separately enables us to use binary search to count the number of flowers blooming at any given time.
- Insight 4: The number of flowers in bloom at time 't' is the number of flowers that have started blooming by time 't' minus the number of flowers that have already finished blooming by time 't'.
Complexity Analysis
Time Complexity: O(nlogn + mlogn)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Binary Search, Sorting, Prefix Sum, Ordered Set.
Companies
Asked at: Capital One, Databricks, Netflix, PhonePe, Roblox, Samsara.