Skip to the code
cf.
+50 XP

#589 Viewers at Each Query Time

hard

A live-stream dashboard records each viewer's session as [start, end] in seconds; the viewer is watching at every second from start to end, both included. Analysts then ask about specific moments, listed in times.

Write viewersAt(sessions, times) that returns an array where item i is the number of sessions watching at second times[i]. Answer in the same order as times.

Example 1

Input: sessions = [[1,6],[3,7],[9,12],[4,13]], times = [2,3,7,11]
Output: [1,2,2,2]

At second 7, the sessions [3, 7] and [4, 13] are watching.

Example 2

Input: sessions = [[1,10],[3,3]], times = [3,3,2]
Output: [2,2,1]

[3, 3] only watches at second 3.

Constraints

  • 0 <= sessions.length, times.length <= 50000
  • 0 <= start <= end <= 10^9
  • 0 <= times[i] <= 10^9
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).