forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideo Stitching.java
31 lines (31 loc) · 1008 Bytes
/
Video Stitching.java
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
class Solution {
public int videoStitching(int[][] clips, int time) {
Arrays.sort(clips , (x , y) -> x[0] == y[0] ? y[1] - x[1] : x[0] - y[0]);
int n = clips.length;
int interval[] = new int[2];
int cuts = 0;
while(true){
cuts++;
int can_reach = 0;
for(int i = interval[0]; i <= interval[1]; i++){
int j = 0;
while(j < n){
if(clips[j][0] < i){
j++;
}
else if(clips[j][0] == i){
can_reach = Math.max(can_reach , clips[j][1]);
j++;
}
else{
break;
}
}
if(can_reach >= time) return cuts;
}
interval[0] = interval[1] + 1;
interval[1] = can_reach;
if(interval[0] > interval[1]) return -1;
}
}
}