KatInterview

Bouquet Sales: Count Days With Enough Flowers

Arrays & HashingAuto-graded · 17 test cases
EasyAuto-gradedArrays & Hashingarraysdifference arrayprefix sums

Last 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-1
    • 1 ≤ 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 to threshold.

Task

Return the number of days (out of n) whose total flower count after all deliveries is ≥ threshold.

Constraints

  • 1 ≤ n ≤ 2000
  • 0 ≤ 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.