LeetCode 1024. Video Stitching

Problem Statement

Greedy.


impl Solution {
pub fn video_stitching(clips: Vec<Vec<i32>>, t: i32) -> i32 {
let mut input = clips;
input.sort_by_key(|x| x[0]);
let (mut cur_end, mut max_end, mut ans, mut j) = (0, 0, 0, 0);
for i in 0..input.len() {
if i < j {
continue;
}
if input[i][0] > cur_end {
return -1;
}
max_end = cur_end;
j = i;
while j < input.len() {
if input[j][0] > cur_end {
break;
}
max_end = std::cmp::max(max_end, input[j][1]);
j += 1;
}
ans += 1;
cur_end = max_end;
if max_end >= t {
return ans;
}
}

return -1;
}
}