Skip to content

Commit

Permalink
Update 1024.Video-Stitching.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Dec 24, 2020
1 parent e71bf37 commit b1a2b7d
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions Greedy/1024.Video-Stitching/1024.Video-Stitching.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
class Solution {
static bool cmp(vector<int>&a, vector<int>&b)
{
if (a[0]==b[0])
return a[1]>b[1];
else
if (a[0]!=b[0])
return a[0]<b[0];
else
return a[1]>b[1];
}
public:
int videoStitching(vector<vector<int>>& clips, int T)
{
sort(clips.begin(),clips.end(),cmp);
if (T==0) return 0;
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)
int far = 0;
int i = 0;
int count = 0;

while (i < clips.size())
{
int nextFar = far;
while (i<clips.size() && clips[i][0]<=far)
{
farReach = max(farReach, clips[idx][1]);
idx++;
nextFar = max(nextFar, clips[i][1]);
i++;
}
if (farReach == right)
return -1;

count++;

if (farReach>=T) return count;
right = farReach;
}

return -1;


if (nextFar >= T)
return count;
else if (nextFar == far)
return -1;
far = nextFar;
}

return -1;
}
};

0 comments on commit b1a2b7d

Please sign in to comment.