Skip to the code
cf.
+10 XP

#584 Total Time Covered by Selections

easy

In a video editor, a user selects several parts of the timeline. Each selection is [start, end] in seconds and covers the time from start up to end (a selection [2, 5] covers 3 seconds). Selections can overlap.

Write coveredSeconds(selections) that returns the total number of seconds covered by at least one selection. Overlapping time is counted once.

Example 1

Input: selections = [[1,4],[2,6],[8,10]]
Output: 7

[1, 4] and [2, 6] cover 1 to 6 (5 seconds), and [8, 10] adds 2.

Example 2

Input: selections = [[0,10],[2,3]]
Output: 10

The second selection is inside the first.

Constraints

  • 0 <= selections.length <= 100000
  • 0 <= start < end <= 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).