static bool cmp(vector<int> a, vector<int> b) //比较函数
{
return a[0] < b[0];
}
int videoStitching(vector<vector<int>> &clips, int T)
{
int len = clips.size(), res = 0;
if (len == 0)
{
return 0;
}
//clips按区间的开始时间排序
sort(clips.begin(), clips.end(), cmp);
//now表示当前遍历的区间,start表示上一个区间的结束时间,end表示所遍历的最大结束时间
int start = 0, end = 0, now = 0;
while (end < T) //只要end没到T,就继续遍历
{
//如果当前区间的开始时间大于上个区间的结束时间,说明中间差了一段,直接返回-1;
if (clips[now][0] > end)
{
return -1;
}
//在小于上去区间的结束时间的那些区间里找结束时间最大的区间
while (now < len && clips[now][0] <= start)
{
if (clips[now][1] > end)
{
end = clips[now][1];
}
now++;
}
//找到最后都没能超过T,则返回-1
if (now == len && end < T)
{
return -1;
}
res++;
start = end;
}
if (end >= T)
{
return res;
}
else
{
return -1;
}
}
Leetcode每日一题:1024.video-stitching(视频拼接)
原创
©著作权归作者所有:来自51CTO博客作者Luweir的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
LeetCode:每日一题(2020.4.13)
回文数
回文数 i++ 两个指针 数组 字符串数组 -
LeetCode:每日一题(2020.4.8)
无重复字符的最长子串
最长子串 i++ 滑动窗口 固定宽度 字符串 -
LeetCode:每日一题(2020.4.9)
寻找两个有序数组的中位数
有序数组 中位数 归并排序 时间复杂度 数组长度 -
LeetCode:每日一题(2020.4.15)
合并两个有序链表
链表 升序 递归 C -
LeetCode:每日一题(2020.4.14)
罗马数字转整数
罗马数字 i++ C -
[leetcode每日一题]11.4
754. 到达终点数字
leetcode每日一题 -
[leetcode每日一题]11.13
791.自定义字符串排序
lambda表达式