Skip to the code
cf.
+25 XP

#723 Autosave Each Document Separately

medium

A note-taking app has several documents open in tabs, and each document is debounced on its own: an edit to a document cancels that document's pending save and schedules a new one wait ms later. Edits to one document never affect another.

edits is a list of [time, docId] pairs sorted by time. A save due at exactly the time of an edit to the same document happens before that edit. Write autosaves(edits, wait) that returns all saves as [time, docId] pairs, sorted by time and then by docId (character-code order).

Example 1

Input: edits = [[0,"a"],[100,"b"],[150,"a"],[900,"b"]], wait = 500
Output: [[600,"b"],[650,"a"],[1400,"b"]]

a is edited at 0 and 150, so it saves at 650. b saves at 600, then again at 1400.

Example 2

Input: edits = [[0,"x"],[0,"y"]], wait = 10
Output: [[10,"x"],[10,"y"]]

Two saves at the same time are ordered by docId.

Constraints

  • 0 <= edits.length <= 10^5
  • Times are whole numbers from 0 to 10^9, sorted ascending; docIds are non-empty strings.
  • 1 <= wait <= 10^6
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).