class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int ans = solution.videoStitching(new int[][]{
{8,10},{17,39},{18,19},{8,16},{13,35},{33,39},{11,19},{18,35}
},20);
System.out.println(ans);
}
public int videoStitching(int[][] clips, int time) {
int len = clips.length;
int t = time;
boolean first = true;
int ans = 0;
while (t != 0) {
int index = selectOne(clips,t,first);
if (index == -1){
return -1;
}
first = false;
t = clips[index][0];
ans++;
}
return ans;
}
public int selectOne(int[][] clips,int t,boolean first){
int dis = Integer.MAX_VALUE;
int ans = -1;
for(int i = 0;i<clips.length;i++){
int[] arr = clips[i];
if(arr[1] >= t && ( (first && arr[0] <= t) || (!first && arr[0] < t) )&& arr[0] < dis) {
dis = arr[0];
ans = i;
}
}
return ans;
}
}