[LeetCode] 1024. Video Stitching

You are given a series of video clips from a sporting event that lasted time seconds. These video clips can be overlapping with each other and have varying lengths.

Each video clip is described by an array clips where clips[i] = [starti, endi] indicates that the ith clip started at starti and ended at endi.

We can cut these clips into segments freely.

  • For example, a clip [0, 7] can be cut into segments [0, 1] + [1, 3] + [3, 7].

Return the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event [0, time]. If the task is impossible, return -1.

Example 1:

Input: clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], time = 10
Output: 3
Explanation: We take the clips [0,2], [8,10], [1,9]; a total of 3 clips.
Then, we can reconstruct the sporting event as follows:
We cut [1,9] into segments [1,2] + [2,8] + [8,9].
Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 10].

Example 2:

Input: clips = [[0,1],[1,2]], time = 5
Output: -1
Explanation: We cannot cover [0,5] with only [0,1] and [1,2].

Example 3:

Input: clips = [[0,1],[6,8],[0,2],[5,6],[0,4],[0,3],[6,7],[1,3],[4,7],[1,4],[2,5],[2,6],[3,4],[4,5],[5,7],[6,9]], time = 9
Output: 3
Explanation: We can take clips [0,4], [4,7], and [6,9].

Constraints:

  • 1 <= clips.length <= 100
  • 0 <= starti <= endi <= 100
  • 1 <= time <= 100

视频拼接。

你将会获得一系列视频片段,这些片段来自于一项持续时长为 time 秒的体育赛事。这些片段可能有所重叠,也可能长度不一。

使用数组 clips 描述所有的视频片段,其中 clips[i] = [starti, endi] 表示:某个视频片段开始于 starti 并于 endi 结束。

甚至可以对这些片段自由地再剪辑:

例如,片段 [0, 7] 可以剪切成 [0, 1] + [1, 3] + [3, 7] 三部分。
我们需要将这些片段进行再剪辑,并将剪辑后的内容拼接成覆盖整个运动过程的片段([0, time])。返回所需片段的最小数目,如果无法完成该任务,则返回 -1 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/video-stitching
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是贪心。这道题跟 1326 很像,但是比 1326 要简单一些。

我用一个数组 maxend 记录每个 clip 最多能覆盖到的右边界在哪里。举个例子,例子三里有 [1, 3], [1, 4] 和 [2, 5], [2, 6] 这样的数据,对于起始点 1 和起始点 2 来说,他们最远能覆盖到的右边界分别是 4 和 6。接着我们开始遍历每一个时间点,如果当前这个时间点在 maxend 有值,则把 last 更新一下,意思是有一个 clip 能覆盖从第 i 分钟到第 last 分钟。如果 i == last,说明当前这个时间往后一定有内容无法被覆盖,返回 -1。如果i == pre,说明当前走到了上一个 clip 能覆盖到的右边界了,此时一定要再加一个 clip,所以 count++,并且把 pre 更新为 last。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int videoStitching(int[][] clips, int time) {
 3         // corner case
 4         if (clips == null) {
 5             return 0;
 6         }
 7 
 8         // normal case
 9         // 用于保存 以当前数字(下标)为起点 的区间的 最大的结束位置
10         int[] maxend = new int[time];
11         for (int[] clip : clips) {
12             if (clip[0] < time) {
13                 maxend[clip[0]] = Math.max(maxend[clip[0]], clip[1]);
14             }
15         }
16 
17         int pre = 0;
18         int last = 0;
19         int count = 0;
20         for (int i = 0; i < time; i++) {
21             last = Math.max(last, maxend[i]);
22             if (i == last) {
23                 return -1;
24             }
25             if (i == pre) {
26                 count++;
27                 pre = last;
28             }
29         }
30         return count;
31     }
32 }

 

LeetCode 题目总结

posted @ 2023-02-21 08:17  CNoodle  阅读(81)  评论(0编辑  收藏  举报