|
| 1 | +# Number of Flowers in Full Bloom |
| 2 | + |
| 3 | +You are given a 0-indexed 2D integer array, flowers, where each element flowers[i]=[starti ,endi] represents the time |
| 4 | +interval during which the ith flower is in full bloom (inclusive). |
| 5 | + |
| 6 | +You are also given a 0-indexed integer array, people, of size n, where people[i] denotes the time at which the ith person |
| 7 | +arrives to view the flowers. For each person, determine how many flowers are in full bloom at their arrival time. |
| 8 | +Return an integer array, ans, of length n, where ans[i] is the number of blooming flowers when the ith person arrives. |
| 9 | + |
| 10 | +## Constraints |
| 11 | + |
| 12 | +- 1 <= flowers.length <= 10^3 |
| 13 | +- `flowers[i].length` == 2 |
| 14 | +- 1 <= starti <= endi <= 10^4 |
| 15 | +- 1 <= people.length <= 10^3 |
| 16 | +- 1 <= people[i] <= 10^4 |
| 17 | + |
| 18 | +### Solution |
| 19 | + |
| 20 | +The core intuition behind this solution is to avoid directly simulating flower blooming across all possible time values, |
| 21 | +which would be inefficient. Instead, we transform the problem into one of counting intervals using binary search. The |
| 22 | +challenge is to determine a person’s arrival time. The difference between these two counts gives the number of flowers |
| 23 | +in bloom at that moment. |
| 24 | + |
| 25 | +To achieve this, the solution separates the flowers’ intervals into two lists: one for start times and one for end times. |
| 26 | +The key trick is that the end times are stored as endi+1 instead of endi. This ensures that when a person arrives at |
| 27 | +exactly endi, the flower is still counted as blooming. With both lists sorted, we can use binary search to quickly |
| 28 | +determine counts for any arrival time. |
| 29 | + |
| 30 | +Let’s break down the key steps of the solution: |
| 31 | + |
| 32 | +1. Initialize the starts and ends arrays for storing start and end times, respectively. |
| 33 | +2. Iterate over each flower interval [starti, endi]. |
| 34 | + - Add starti to the starts list. |
| 35 | + - Add endi+1 to the ends list. |
| 36 | +3. Sort both lists to ensure that binary search can efficiently count the number of flowers that started or ended before |
| 37 | + a given arrival time. |
| 38 | +4. Create an ans list of size n to store the answer for each person. |
| 39 | +5. Iterate over people array and for each person’s arrival time, perform two binary searches: |
| 40 | + - On starts, find how many flowers began blooming at or before the arrival time. |
| 41 | + - On ends, find how many flowers had already finished blooming before the arrival time. |
| 42 | + - Store the difference between the counts in the ans array. |
| 43 | +6. After completing the iteration, return the ans array as the output. |
| 44 | + |
| 45 | +Binary Search implementation details: |
| 46 | +- The binarySearch function is a modified version of the standard algorithm. |
| 47 | +- It returns the index of the first element greater than the target (upper bound). |
| 48 | +- This effectively gives the count of values ≤ target. |
| 49 | + |
| 50 | +### Time Complexity |
| 51 | + |
| 52 | +- Creating the starts and ends arrays of length n takes O(n) time. |
| 53 | +- Sorting both arrays costs O(nlogn). |
| 54 | +- Next, for each of the m people, we perform two binary searches on these arrays, each taking O(logn) time. This |
| 55 | +contributes O(mlogn) in total. |
| 56 | + |
| 57 | +So, the overall time complexity is O(nlogn+mlogn), which simplifies to O((n+m)logn). |
| 58 | + |
| 59 | +### Space Complexity |
| 60 | + |
| 61 | +The space complexity is O(n) because the solution stores all n flowers’ start and end times in separate lists (starts |
| 62 | +and ends). So, the overall space complexity is O(n). |
0 commit comments