Show problems as
Classic problem #2251HardArrayHash TableBinary SearchSortingPrefix SumOrdered Set

Number of Flowers in Full Bloom

Real-world version: Stalls Trading When the Visitor Arrives
Tracked in this browser only
Read Number of Flowers in Full Bloom on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A night market runs on a single long clock measured in whole minutes. stalls[i] = [open_i, shut_i] says that stall i is trading during every minute from open_i to shut_i, and both of those minutes count: the stall is trading at minute open_i and still trading at minute shut_i.

arrivals[j] is the minute at which visitor j walks in.

Return a list answer where answer[j] is the number of stalls trading at minute arrivals[j]. Keep the answers in the same order as arrivals. Several visitors may arrive in the same minute.

Examples

Example 1

Input
stalls = [[3, 7], [5, 9], [8, 8]], arrivals = [3, 7, 8, 9, 10, 2]
Output
[1, 2, 2, 1, 0, 0]

At minute 3 only the first stall has opened. At minute 7 the first and second are both trading. At minute 8 the first has shut but the second and third are trading. At minute 9 only the second is left, and at minutes 10 and 2 nothing is trading.

Example 2

Input
stalls = [[4, 4]], arrivals = [4, 3, 5]
Output
[1, 0, 0]

The single stall opens and shuts within minute 4, so it is trading for the visitor who arrives then and for nobody else.

Example 3

Input
stalls = [[2, 6], [3, 5], [4, 4]], arrivals = [4, 6, 2, 1]
Output
[3, 1, 1, 0]

Minute 4 falls inside all three trading spans. Minute 6 is the last minute of the first stall only, minute 2 is the first minute of the first stall only, and minute 1 is before every stall opens.

Constraints

  • 1 <= stalls.length <= 5 * 10^4
  • stalls[i].length == 2
  • 1 <= stalls[i][j] <= 10^9
  • A stall never shuts before it opens.
  • 1 <= arrivals.length <= 5 * 10^4
  • 1 <= arrivals[i] <= 10^9

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def stalls_open(stalls: list[list[int]], arrivals: list[int]) -> list[int]:
Java
public int[] stallsOpen(int[][] stalls, int[] arrivals)