Opening the reading…
Opening the reading…
PREFIX SUM › LINE SWEEP
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →At time t, a flower contributes exactly when its start is at most t and its end is at least t. Count all flowers whose start is at most t, then remove the flowers whose end is strictly before t. The word strictly matters: a flower ending at t is still blooming when the person arrives at t.
This separates one interval query into two one-dimensional queries. Store every start time in one sorted array and every end time in another. For a person arriving at t, upper_bound on starts counts values at most t, while lower_bound on ends counts values strictly less than t. Their difference is the number still in bloom.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(m log m + n log m) | Copying endpoints takes O(m), sorting the two endpoint arrays costs O(m log m), and each of the n people performs two binary searches over arrays of length m. The searches examine logarithmically many positions rather than revisiting flowers. |
| Space | O(m) extra | The two sorted endpoint arrays store 2m values, which is O(m), and the answer is required output and excluded from the extra-space bound. The bound stays O(m) even when all intervals share the same endpoints or every person arrives at the same time. |
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& people) {
vector<int> starts;
vector<int> ends;
for (const vector<int>& flower : flowers) {
starts.push_back(flower[0]);
ends.push_back(flower[1]);
}
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
vector<int> answer;
for (int t : people) {
int started = upper_bound(starts.begin(), starts.end(), t) - starts.begin();
int ended = lower_bound(ends.begin(), ends.end(), t) - ends.begin();
answer.push_back(started - ended);
}
return answer;
}
};The two binary-search choices encode the interval's inclusive right endpoint. upper_bound returns the first start greater than t, so its position counts every start at most t. lower_bound returns the first end at least t, so its position counts only ends below t. Replacing either boundary changes the meaning of the interval and fails exactly when an arrival matches a flower endpoint.
You can sort the people together with their original indices and sweep through time once. Maintain a pointer over starts and add every flower beginning by the current person's time. Maintain another pointer over ends and remove every flower whose end is strictly less than that time. This is not generally an optimisation: it uses O(m + n) working storage and O(m log m + n log n) time, but it can be useful when you want one chronological pass over all queries.
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& people) {
vector<int> starts;
vector<int> ends;
for (const vector<int>& flower : flowers) {
starts.push_back(flower[0]);
ends.push_back(flower[1]);
}
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
vector<pair<int, int>> queries;
for (int i = 0; i < static_cast<int>(people.size()); ++i) {
queries.push_back({people[i], i});
}
sort(queries.begin(), queries.end());
vector<int> answer(people.size());
int started = 0;
int ended = 0;
for (const auto& query : queries) {
int time = query.first;
while (started < static_cast<int>(starts.size()) && starts[started] <= time) {
++started;
}
while (ended < static_cast<int>(ends.size()) && ends[ended] < time) {
++ended;
}
answer[query.second] = started - ended;
}
return answer;
}
};