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 timeclips[start, end]time
Example 1
Output: 3
Explanation: [0, 2], [1, 9] and [8, 10] together cover 0 to 10.
Example 2
Output: -1
Explanation: Nothing covers the part from 2 to 5.
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.
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
For Example 1, reachFromtimetimetime
When to add a clip
When exactly does the video need another clip? Walk a moment ttime - 1coveredfarthestt
At each tfarthestreachFrom[t]tcoveredfarthestcoveredfarthest
In Example 1 that happens at tfarthestt
Noticing a gap
What if no clip carries the video past ttcoveredfarthesttcoveredtttfarthest
The same check catches clips that miss the very beginning. If no clip starts at 0, then at tfarthest
A slower way: a table of answers
The answer can also be built from answers for shorter stretches. Let dp[t]tdp[0]
A clip [s, e] with s < t ≤ e can finish the stretch to tdp[s]dp[t]dp[s] + 1dp[1]dp[8]dp[1]dp[10]dp[8]
Building dp[10]dp[8]dp[8]dp[1]ttime
Java Solution
The method videoStitching
- First, create
, an array ofreachFrom numbers. A loop overtime + 1 skips any clip that starts afterclips . For each other clip, it storestime at the clip's start, if that beats the end already stored there.Math.min(clip[1], time) - Set three numbers to 0:
, how many clips are chosen;used , where those clips end; andcovered , the best end seen so far.farthest - A second loop moves
from 0 up tot . At eachtime - 1 ,t becomes the larger of itself andfarthest .reachFrom[t] - When
equalst , check for a gap: ifcovered isfarthest or less, return -1. Otherwise add one tot and setused tocovered .farthest - If the walk gets through every
without a gap, returnt , the number of clips chosen.used
The main
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:
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 timet
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
Why does the walk stop at t < timet <= timetcoveredfarthest
Complexity
The work is one pass over the clips to fill reachFrom
- Time: O(n + time), because filling
looks at each of the n clips once, and the walk visits each moment from 0 toreachFrom once, with a few comparisons at each.time - 1 - Space: O(time), because
keeps one farthest end for each start time from 0 toreachFrom , while the walk itself keeps only three numbers.time
Watering a garden with the fewest taps turns into the same walk once each tap's reach is known: see