Skip to the code
cf.
+50 XP

#12 Toast Notification Timeline

hard

A web app shows toast notifications in a corner, but at most k can be on screen at once. Toast i is requested at time arrivals[i] and stays on screen for durations[i] seconds once it appears.

Toasts appear in request order (first come, first served). Toast i appears at the earliest time t that is at or after its request, at or after the moment the toast before it appeared, and when fewer than k earlier toasts are still on screen. A toast that appeared at time s with duration d is on screen for times s <= t < s + d, so its slot is free again at exactly s + d.

Write toastEnds(arrivals, durations, k) that returns, for each toast, the time it disappears.

Example 1

Input: arrivals = [0,1,2,3], durations = [5,2,4,1], k = 2
Output: [5,3,7,6]

Toasts 0 and 1 show right away. Toast 2 waits for toast 1 to leave at 3 and shows until 7. Toast 3 waits for toast 0 to leave at 5.

Example 2

Input: arrivals = [0,0,0], durations = [2,2,2], k = 1
Output: [2,4,6]

Only one toast fits, so each one waits for the one before it.

Constraints

  • 0 <= arrivals.length <= 1000 and durations.length === arrivals.length
  • arrivals is sorted ascending (ties keep index order).
  • 1 <= durations[i] <= 10^6, 0 <= arrivals[i] <= 10^9, 1 <= k <= 1000
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).