Video Stitching

Find the fewest video clips that cover a whole match, by always taking the clip that reaches farthest.

A sporting event runs from moment 0 to moment time, and it was recorded in several video clips. Each clip is a pair [start, end] of whole numbers, and clips may overlap. A clip can be trimmed into pieces, so the overlapping parts cost nothing. Return the fewest clips that together cover the whole event, from 0 to time, or -1 if the clips cannot cover it.

Example 1
Input: clips = [[0, 2], [4, 6], [8, 10], [1, 9], [1, 5], [5, 9]], time = 10
Output: 3
Explanation: [0, 2], [1, 9] and [8, 10] together cover 0 to 10.
Example 2
Input: clips = [[0, 1], [1, 2]], time = 5
Output: -1
Explanation: Nothing covers the part from 2 to 5.
Solution

The Idea

Imagine a match filmed by several people in the crowd. Each phone recorded a stretch of the game, some stretches overlap, and none covers all of it. To make one video of the whole match, you stitch clips together and trim the overlaps. The question is how few clips can do it.

In Example 1 the match runs from 0 to 10, and there are six clips. The video has to begin with a clip that starts at 0, and [0, 2] is the one clip that does. With it, the video is covered up to 2. Which clip should come next?

Any clip that starts at 2 or earlier joins on without leaving a hole, so [1, 9] and [1, 5] both qualify. The clip [4, 6] does not, because the part from 2 to 4 would be missing. Of the two that qualify, [1, 9] carries the video to 9 and [1, 5] only to 5.

Reaching 9 can never be worse than reaching 5. Any clip that could follow [1, 5] starts at 5 or earlier, so it also starts at 9 or earlier and could follow [1, 9] just as well. Look at the picture below.

Example 1 on a timeline from 0 to 10. The six clips are bars. Pick 1 is [0, 2], so covered = 2. Among clips starting at or before 2, [1, 9] reaches farthest: pick 2, covered = 9. Among clips starting at or before 9, [8, 10] reaches farthest: pick 3, covered = 10.

Each bar is a clip, placed over the timeline from its start to its end. The dashed teal line labelled covered = 2 marks where pick 1, [0, 2], ends. Of the bars that start at or left of that line, [1, 9] reaches farthest, so it becomes pick 2, and the video is now covered up to the line labelled covered = 9.

Of the bars that start at or before 9, [8, 10] reaches 10, the end of the match. That is pick 3, and three clips is the answer. Notice that [8, 10] starts at 8, inside the part already covered, and the overlap from 8 to 9 is simply trimmed away.

So each step takes, among the clips that join on to the covered part, the one that reaches farthest. Picking the next clip by what looks best right now, without trying the other clips, is called a greedy choice. Here it is safe for the reason above: a clip that reaches farther keeps every later clip available.

Example 2 shows how the walk can fail. [0, 1] and [1, 2] cover the match up to 2, but no clip starts at 2 or earlier and goes past 2. The part from 2 to 5 stays empty, so the answer is -1.

Solution Approach

Turning the greedy choice into code means settling three things: how to find the farthest-reaching clip quickly, when to add a clip, and how to notice a gap. A slower method that fills a table of answers reaches the same results and is worth knowing too.

The farthest end for each start time

How do we find the clip that reaches farthest, at each pick? Checking every clip at every pick would work, but a small array gives the answer directly. Clip times are whole numbers, so an array named reachFrom can hold, at index s, the farthest end of any clip that starts at s.

For Example 1, reachFrom holds 2 at index 0, 9 at index 1, 6 at index 4, 9 at index 5 and 10 at index 8, and 0 everywhere else. Two clips start at 1, [1, 9] and [1, 5], and only the better end, 9, is kept. A clip that starts after time cannot help and is skipped. An end past time is cut down to time, since nothing after the match matters.

When to add a clip

When exactly does the video need another clip? Walk a moment t through the match, from 0 up to time - 1, keeping two numbers. The number covered is where the chosen clips end so far, and farthest is the best end of any clip that starts at or before t.

At each t, farthest takes in reachFrom[t]. When t reaches covered, the chosen clips run out right here, so one more clip is needed, and the best one ends at farthest. The count of clips goes up by one, and covered jumps to farthest.

In Example 1 that happens at t = 0, 2 and 9, which are pick 1, pick 2 and pick 3 in the picture. Between those moments, farthest keeps growing quietly: it becomes 9 at t = 1, long before pick 2 needs it.

Noticing a gap

What if no clip carries the video past t? Then, when t reaches covered, farthest is still t or less, and the answer is -1. In Example 2, covered becomes 1 at t = 0 and 2 at t = 1. At t = 2, farthest is still 2, which is the gap.

The same check catches clips that miss the very beginning. If no clip starts at 0, then at t = 0, farthest is 0, and the method returns -1 right away.

A slower way: a table of answers

The answer can also be built from answers for shorter stretches. Let dp[t] be the fewest clips that cover the stretch from 0 to t, with dp[0] = 0, because an empty stretch needs no clip.

A clip [s, e] with s < t ≤ e can finish the stretch to t, after dp[s] clips have covered 0 to s. So dp[t] is the smallest dp[s] + 1 over all such clips. In Example 1, dp[1] is 1 thanks to [0, 2]. The clip [1, 9] then gives dp[8] = dp[1] + 1 = 2, and [8, 10] gives dp[10] = dp[8] + 1 = 3.

Building dp[10] from dp[8], and dp[8] from dp[1], each entry from earlier ones, is called dynamic programming. It looks at every clip for every t, so it takes about time × (number of clips) steps. The greedy walk needs one pass over the clips and one over the timeline instead.

Java Solution

The method videoStitching works in these steps:

  1. First, create reachFrom, an array of time + 1 numbers. A loop over clips skips any clip that starts after time. For each other clip, it stores Math.min(clip[1], time) at the clip's start, if that beats the end already stored there.
  2. Set three numbers to 0: used, how many clips are chosen; covered, where those clips end; and farthest, the best end seen so far.
  3. A second loop moves t from 0 up to time - 1. At each t, farthest becomes the larger of itself and reachFrom[t].
  4. When t equals covered, check for a gap: if farthest is t or less, return -1. Otherwise add one to used and set covered to farthest.
  5. If the walk gets through every t without a gap, return used, the number of clips chosen.

The main method runs both examples, two clips that only touch at 1, clips with nothing at 0, one clip longer than the match, and a match of length 0.

Java
public class VideoStitching {

    public static int videoStitching(int[][] clips, int time) {
        int[] reachFrom = new int[time + 1];  // farthest end of a clip starting at t
        for (int[] clip : clips) {
            if (clip[0] <= time) {
                reachFrom[clip[0]] = Math.max(reachFrom[clip[0]], Math.min(clip[1], time));
            }
        }
        int used = 0;
        int covered = 0;   // the chosen clips cover [0, covered]
        int farthest = 0;  // how far one more clip could reach
        for (int t = 0; t < time; t++) {
            farthest = Math.max(farthest, reachFrom[t]);
            if (t == covered) {
                if (farthest <= t) {
                    return -1;          // no clip goes past t: a gap
                }
                used++;
                covered = farthest;     // take the clip that reaches farthest
            }
        }
        return used;
    }

    public static void main(String[] args) {
        int[][] example1 = {{0, 2}, {4, 6}, {8, 10}, {1, 9}, {1, 5}, {5, 9}};
        System.out.println(videoStitching(example1, 10));
        System.out.println(videoStitching(new int[][] {{0, 1}, {1, 2}}, 5));
        System.out.println(videoStitching(new int[][] {{0, 1}, {1, 2}}, 2));
        System.out.println(videoStitching(new int[][] {{1, 5}, {2, 8}}, 5));
        System.out.println(videoStitching(new int[][] {{0, 100}}, 5));
        System.out.println(videoStitching(new int[][] {{0, 3}}, 0));
    }
}

Running it prints:

Output
3
-1
2
-1
1
0

The 3 and the -1 at the top answer Examples 1 and 2. The third line, 2, comes from [0, 1] and [1, 2] with time = 2: clips that meet at a single moment join without a gap. The fourth line is -1 because [1, 5] and [2, 8] leave 0 to 1 empty, so the gap check fires at t = 0.

The fifth line, 1, is the single clip [0, 100] covering a match of length 5, with its end cut down to 5 in reachFrom. The last line, 0, is a match of length 0: the walk never starts, and no clip is needed.

Why does the walk stop at t < time? With t <= time, Example 1 also visits t = 10. There covered is already 10 and farthest is 10 too, so the gap check fires. Running Example 1 with that change prints -1 instead of 3, although the match is fully covered.

Complexity

The work is one pass over the clips to fill reachFrom, then one walk along the timeline.

  • Time: O(n + time), because filling reachFrom looks at each of the n clips once, and the walk visits each moment from 0 to time - 1 once, with a few comparisons at each.
  • Space: O(time), because reachFrom keeps one farthest end for each start time from 0 to time, while the walk itself keeps only three numbers.

Watering a garden with the fewest taps turns into the same walk once each tap's reach is known: see Minimum Number of Taps.