#2251
Hard Algorithms Number of flowers in full bloom
Array Hash Table Binary Search Sorting Prefix Sum Ordered Set
57.7% acceptance
Feb 25, 2026
1815
46
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.
Return an integer array answer of size n, where answer[i] is the number of flowers that are in full bloom when the ith person arrives.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn full_bloom_flowers(flowers: Vec<Vec<i32>>, people: Vec<i32>) -> Vec<i32> {
let mut starts: Vec<i32> = flowers.iter().map(|f| f[0]).collect();
let mut ends: Vec<i32> = flowers.iter().map(|f| f[1]).collect();
starts.sort_unstable();
ends.sort_unstable();
people.iter().map(|&t| {
// # flowers started by time t: partition_point where start <= t
let started = starts.partition_point(|&s| s <= t) as i32;
// # flowers ended before time t: partition_point where end < t
let ended = ends.partition_point(|&e| e < t) as i32;
started - ended
}).collect()
}
}