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's daily question: 1024.video-stitching (video stitching)
Guess you like
Origin blog.csdn.net/wyll19980812/article/details/109254645
Ranking