forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1024.Video-Stitching.cpp
40 lines (34 loc) · 956 Bytes
/
1024.Video-Stitching.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
static bool cmp(vector<int>&a, vector<int>&b)
{
if (a[0]==b[0])
return a[1]>b[1];
else
return a[0]<b[0];
}
public:
int videoStitching(vector<vector<int>>& clips, int T)
{
sort(clips.begin(),clips.end(),cmp);
if (clips[0][0]!=0) return -1;
int right = clips[0][1];
int idx = 0;
int count = 1;
if (clips[0][1]>=T) return count;
while (idx < clips.size())
{
int farReach = right;
while (idx<clips.size() && clips[idx][0]<=right)
{
farReach = max(farReach, clips[idx][1]);
idx++;
}
if (farReach == right)
return -1;
count++;
if (farReach>=T) return count;
right = farReach;
}
return -1;
}
};