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;
}
};