Skip to the code
cf.
+25 XP

#157 Events in Time Windows

medium

An analytics dashboard stores the times of all click events in times, sorted ascending. Several clicks can share a time. The user drags out several time windows on a chart, and each window [from, to] should show how many clicks happened from from to to, including both ends.

Write countInWindows(times, windows) that returns an array with one count per window, in the same order as windows.

There can be many windows, so answer each one faster than a scan over times.

Example 1

Input: times = [1,3,3,5,8,13], windows = [[2,5],[6,7]]
Output: [3,0]

Clicks at 3, 3, and 5 fall in [2, 5]. None fall in [6, 7].

Example 2

Input: times = [10,20,30], windows = [[10,30],[0,10],[30,99]]
Output: [3,1,1]

Both ends count, so [10, 30] holds all three clicks.

Constraints

  • 0 <= times.length <= 100000
  • times is sorted ascending.
  • 1 <= windows.length <= 100000
  • from <= to in every window.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).