Leetcode每日一题:1024.video-stitching(视频拼接)

video-stitching在这里插入图片描述

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值