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
| class Solution { public int videoStitching(int[][] clips, int time) { Arrays.sort(clips, (o1, o2) -> { int diff = Integer.compare(o1[0], o2[0]); if (diff != 0) { return diff; } return Integer.compare(o2[1], o1[1]); });
if (clips[0][0] != 0) { return -1; }
int end = 0; int i = 0; int count = 0;
while (i < clips.length && clips[i][0] <= end) { int nextEnd = end; while (i < clips.length && clips[i][0] <= end) { nextEnd = Math.max(nextEnd, clips[i][1]); i++; }
end = nextEnd; count++;
if (end >= time) { return count; } }
return -1; } }
|