Skip to the code
+50 XP

#593 Fewest Clips to Cover the Timeline

hard

A video editor builds a highlight reel from recorded clips. Clip i covers the time from clips[i][0] to clips[i][1] seconds. Clips can be trimmed, and they may overlap.

Write fewestClips(clips, duration) that returns the fewest clips needed to cover the whole timeline from 0 to duration with no gaps, or -1 if it can't be covered. Clips that touch, like [0, 4] and [4, 7], leave no gap.

Example 1

Input: clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], duration = 10
Output: 3

Use [0, 2], [1, 9], and [8, 10].

Example 2

Input: clips = [[0,1],[1,2]], duration = 5
Output: -1

Nothing covers the time after 2.

Constraints

  • 0 <= clips.length <= 100000
  • 0 <= clips[i][0] <= clips[i][1] <= 10^9
  • 1 <= duration <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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