Bouquet Sales: Count Days With Enough Flowers
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests the difference-array technique by efficiently applying ranged inventory updates and then scanning totals to answer threshold queries.
Bouquet Sales: Count Days With Enough Flowers
A flower shop tracks delivery schedules over a season of n days (0-indexed, days 0 through n-1).
You are given a list of deliveries. Each delivery replenishes stock over a contiguous range of days: starting on day start, ending on day end (inclusive), and adding qty flowers to each of those days.
After all deliveries are applied, you need to count how many days have a total flower count of at least threshold.
Write a function that, given the number of days, the list of deliveries, and the threshold, returns the count of days whose total flower count is ≥ threshold.
Input
n— the number of days in the season (1 ≤ n ≤ 2000).deliveries— a list of deliveries, each a list[start, end, qty]where:0 ≤ start ≤ end ≤ n-11 ≤ qty ≤ 10_000- The list may be empty (no deliveries).
threshold— an integer (1 ≤ threshold ≤ 100_000_000). A day qualifies if its total count is strictly greater than or equal tothreshold.
Task
Return the number of days (out of n) whose total flower count after all deliveries is ≥ threshold.
Constraints
1 ≤ n ≤ 20000 ≤ len(deliveries) ≤ 2000- Each delivery:
0 ≤ start ≤ end ≤ n-1,1 ≤ qty ≤ 10_000 1 ≤ threshold ≤ 100_000_000- A day with no deliveries has a flower count of
0.
Examples
Example 1
Input: count_threshold_days(7, [[0, 3, 5], [2, 6, 3], [5, 6, 10]], 8)
Output: 4
Example 2
Input: count_threshold_days(5, [], 1)
Output: 0
Example 3
Input: count_threshold_days(4, [[0, 3, 2], [1, 2, 3]], 5)
Output: 2
Hints
Hint 1
Build a difference array of size n+1: for each delivery [start, end, qty], add qty at index start and subtract qty at index end+1.
Hint 2
Take the prefix sum of the difference array to recover the actual flower count for every day.
Hint 3
Finally, iterate over all n days and count those whose flower count is >= threshold.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Difference Array
Naively, we could loop over each delivery and add qty to every day in [start, end]. With up to 2,000 deliveries each spanning up to 2,000 days, that is O(D × n) = O(4,000,000) — manageable here, but the difference array technique does it cleanly in O(D + n).
Difference array construction:
- Allocate
diff[0..n](sizen+1) initialized to zero. - For each delivery
[start, end, qty]:diff[start] += qty— the addition kicks in starting atstart.diff[end+1] -= qty— the addition stops afterend.
Recovery via prefix sum:
- Iterate
ifrom0ton-1, maintaining a running sum. running += diff[i]gives the exact flower count on dayi.- If
running >= threshold, increment the answer counter.
Why it is correct
The prefix sum of the difference array recovers the original range-updated array exactly. Each delivery's +qty propagates forward from start and is cancelled at end+1, so only days within [start, end] see the increment. Multiple deliveries stack additively.
Complexity
| Value | |
|---|---|
| Time | O(D + n) — one pass to build diff, one pass to scan |
| Space | O(n) — the difference array |
where D = number of deliveries and n = number of days.
Worked example (sample 1)
n=7, deliveries [[0,3,5],[2,6,3],[5,6,10]], threshold 8
Difference array after all deliveries:
index: 0 1 2 3 4 5 6 7
diff: 5 0 3 -5 -3 10 -3 -10 (conceptually)
Wait, let me be precise:
[0,3,5]:diff[0]+=5,diff[4]-=5[2,6,3]:diff[2]+=3,diff[7]-=3[5,6,10]:diff[5]+=10,diff[7]-=10
index: 0 1 2 3 4 5 6 7
diff: 5 0 3 0 -5 10 0 -13
Prefix sums (daily counts):
day 0: 5 day 1: 5 day 2: 8 day 3: 8
day 4: 3 day 5: 13 day 6: 13
Days ≥ 8: days 2, 3, 5, 6 → 4 days.
from typing import List
def count_threshold_days(n: int, deliveries: List[List[int]], threshold: int) -> int:
diff = [0] * (n + 1)
for start, end, qty in deliveries:
diff[start] += qty
diff[end + 1] -= qty
count = 0
running = 0
for i in range(n):
running += diff[i]
if running >= threshold:
count += 1
return count