//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;
}
};
LeetCode1024. 视频拼接
最新推荐文章于 2021-09-23 16:34:38 发布