Number of Flowers in Full Bloom — LeetCode 2251 Python Solution
- Problem
- #2251
- Pattern
- Prefix Sum
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given a 0-indexed 2D integer array flowers, where flowers[i] = [starti, endi] means the ith flower will be in full bloom from starti to endi (inclusive). You are also given a 0-indexed integer array people of size n, where people[i] is the time that the ith person will arrive to see the flowers.
Example
- Input
- flowers = [[1,6],[3,7],[9,12],[4,13]], people = [2,3,7,11]
- Output
- [1,2,2,2]
- Explanation
- The figure above shows the times when the flowers are in full bloom and when the people arrive.
Python solution
class Solution:
def fullBloomFlowers(
self, flowers: List[List[int]], people: List[int]
) -> List[int]:
start, end = sorted(a for a, _ in flowers), sorted(b for _, b in flowers)
return [bisect_right(start, p) - bisect_left(end, p) for p in people]Complexity
| Measure | Complexity |
|---|---|
| Time | O((m + n) \times \log n) |
| Space | O(n) auxiliary |
Pattern: Prefix Sum
Precompute running totals once so any range query becomes a single subtraction. LeetCode 2251. Number of Flowers in Full Bloom is filed here because LeetCode tags it Prefix Sum, which is the vocabulary this hub collects.
The prefix sum guide has the Python template for the pattern and the 157 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2251. Number of Flowers in Full Bloom?
- LeetCode 2251. Number of Flowers in Full Bloom is rated Hard on LeetCode.
- What is the time complexity of LeetCode 2251. Number of Flowers in Full Bloom?
- The Python solution on this page runs in O((m + n) \times \log n).
- What is the space complexity of LeetCode 2251. Number of Flowers in Full Bloom?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 2251. Number of Flowers in Full Bloom cover?
- LeetCode 2251. Number of Flowers in Full Bloom is tagged Array, Hash Table, Binary Search, Ordered Set, Prefix Sum and Sorting on LeetCode.