You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You are given a series of video clips from a sporting event that lasted T seconds. These video clips can be overlapping with each other and have varied lengths.
Each video clip clips[i] is an interval: it starts at time clips[i][0] and ends at time clips[i][1]. 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, T]). If the task is impossible, return -1.
Example 1:
Input: clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], T = 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]], T = 5
Output: -1
Explanation:
We can't 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]], T = 9
Output: 3
Explanation:
We can take clips [0,4], [4,7], and [6,9].
Example 4:
Input: clips = [[0,4],[2,8]], T = 5
Output: 2
Explanation:
Notice you can have extra video after the event ends.
Constraints:
1 <= clips.length <= 100
0 <= clips[i][0] <= clips[i][1] <= 100
0 <= T <= 100
这道题说是给了一些视频片段,是关于长度为T秒的一项体育运动,这些视频片段可能会有时间上的重叠,而且各自的时长不等。每个片段有各自的起始时间和结束时间,说是我们可以任意剪切一个片段为任意段,现在问最少需要拿几个给定的片段可以完整的剪出0到T之间的完整运动录像。题目看不太懂的童鞋,可以看给的例子,其实就是找几个片段,使得它们的并集能覆盖整个0到T的区间就行,具体怎么剪裁并不用管。这道题用贪婪算法和动态规划都是可以做的,先来看贪婪算法,需要先按照起始时间给片段排个序,然后用几个变量,st 表示当前用到的片段可以到达的位置,end 表示新加一个片段可以到达的最大的位置,i表示当前遍历到的片段的坐标。进行 while 循环,条件是 st 小于 T,然后此时检测片段,假如某个片段的起始时间小于等于 st,则可以用其结束位置来更新 end,直到选出一个最大的区间。若此时 st 还是等于 end,说明此时已经断层了,无法覆盖整个区间了,直接返回 -1。否则将 st 更新为 end,结果 res 自增1。继续循环,直至退出,然后返回 res 即可,参见代码如下:
解法一:
class Solution {
public:
int videoStitching(vector<vector<int>>& clips, int T) {
int res = 0, n = clips.size(), i = 0, st = 0, end = 0;
sort(clips.begin(), clips.end());
while (st < T) {
while (i < n && clips[i][0] <= st) {
end = max(end, clips[i++][1]);
}
if (st == end) return -1;
st = end;
++res;
}
return res;
}
};
You are given a series of video clips from a sporting event that lasted
T
seconds. These video clips can be overlapping with each other and have varied lengths.Each video clip
clips[i]
is an interval: it starts at timeclips[i][0]
and ends at timeclips[i][1]
. 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, T]
). If the task is impossible, return-1
.Example 1:
Example 2:
Example 3:
Example 4:
Constraints:
1 <= clips.length <= 100
0 <= clips[i][0] <= clips[i][1] <= 100
0 <= T <= 100
这道题说是给了一些视频片段,是关于长度为T秒的一项体育运动,这些视频片段可能会有时间上的重叠,而且各自的时长不等。每个片段有各自的起始时间和结束时间,说是我们可以任意剪切一个片段为任意段,现在问最少需要拿几个给定的片段可以完整的剪出0到T之间的完整运动录像。题目看不太懂的童鞋,可以看给的例子,其实就是找几个片段,使得它们的并集能覆盖整个0到T的区间就行,具体怎么剪裁并不用管。这道题用贪婪算法和动态规划都是可以做的,先来看贪婪算法,需要先按照起始时间给片段排个序,然后用几个变量,st 表示当前用到的片段可以到达的位置,end 表示新加一个片段可以到达的最大的位置,i表示当前遍历到的片段的坐标。进行 while 循环,条件是 st 小于 T,然后此时检测片段,假如某个片段的起始时间小于等于 st,则可以用其结束位置来更新 end,直到选出一个最大的区间。若此时 st 还是等于 end,说明此时已经断层了,无法覆盖整个区间了,直接返回 -1。否则将 st 更新为 end,结果 res 自增1。继续循环,直至退出,然后返回 res 即可,参见代码如下:
解法一:
我们也可以用动态规划 Dynamic Programming 来做,定义一个一维的 dp 数组,其中 dp[i] 表示覆盖 [0, i] 区间需要的最少片段个数,dp 大小定为 T+1,初始化均为 T+1。将 dp[0] 更新为0,这里还是先给片段按起始时间排个序,遍历所有的片段,然后遍历该片段时间区域内的所有时间点,注意,因为题目中也说了片段的时间范围可能大于T,为了避免越界,需要判断一下若i大于T,直接 break 掉循环,否则就可以用
dp[clip[0]]+1
来更新其 dp 值,参见代码如下:解法二:
下面这种也是 DP 解法,不过并不用给片段排序,因为 dp 的更新方法不同,定义还是跟上面相同,不过这里就可以定义为 T+1 的大小,且均初始化为 T+1,除了 dp[0] 要赋值为0。然后此时是从1遍历到T,对于每个时间点,遍历所有的片段,假如当前时间点i在该片段中间,则用
dp[clip[0]]+1
来更新 dp[i],注意和上面解法的不同之处,参见代码如下:解法三:
Github 同步地址:
#1024
参考资料:
https://leetcode.com/problems/video-stitching/
https://leetcode.com/problems/video-stitching/discuss/269988/C%2B%2BJava-6-lines-O(n-log-n)
https://leetcode.com/problems/video-stitching/discuss/270036/JavaC%2B%2BPython-Greedy-Solution-O(1)-Space
LeetCode All in One 题目讲解汇总(持续更新中...)
(欢迎加入博主的知识星球,博主将及时答疑解惑,并分享刷题经验与总结,快快加入吧~)
微信打赏
Venmo 打赏
---|---
The text was updated successfully, but these errors were encountered: