LeetCode1024. 视频拼接

//1024. 视频拼接
/*
1、先将没用的删除
  1)比如:[1,10],[1,3]
  2)比如:[1,10],[2,4]
  上面两种,可以这样删除
    1)将原数组排序,排序依据为:
       判断起始是否相等
        -相等:以差值降序排列
        -不等:以起始位置升序排列
      排序之后的例子:
        [0,10]
        [0,7]
        [0,2]
        [1,7]
        [1,3]
        [2,11]
        [2,5]
    2)之后再判断相邻的两个是否符合,即可
2、然后贪心:
  每次遍历:找出符合条件的,能走最远的方案,
  过程中计数即可
    例子:
    [0,10],[2,20],[3,30],[4,50],[11,60]
    这种情况,先找到[0,10]
        然后找到[4,50]
        再找到[11,60]
*/
class Solution {
public:
  static bool cmp(const vector<int>& a, const vector<int>& b) {
    if (a[0] == b[0]) return  a[1] - a[0] > b[1] - b[0];
    return a[0] < b[0];
  }
  int videoStitching(vector<vector<int>>& clips, int T) {
    if (T == 0) return 0;
    if (clips.empty()) return -1;
    sort(clips.begin(), clips.end(), cmp);
    deque<vector<int>>s;
    s.push_back(clips.front());
    for (int i = 1; i < clips.size(); i++)
      if (clips[i][0] != s.back()[0] && clips[i][1] > s.back()[1])
        s.push_back(clips[i]);
    int cnt = 0;
    int l = 0;
    int r = 0;
    while (r < T) {
      for (auto x : s)
        if (l >= x[0] && r < x[1]) r = x[1];
      if (r == l) return -1;
      l = r;
      cnt++;
    }
    return cnt;
  }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值