class Solution:
def videoStitching(self, clips, time):
clips.sort()
res = 0
curr_end = 0
next_end = 0
i = 0
n = len(clips)
while curr_end < time:
while i < n and clips[i][0] <= curr_end:
next_end = max(next_end, clips[i][1])
i += 1
if curr_end == next_end:
return -1
res += 1
curr_end = next_end
return res
class Solution {
public:
int videoStitching(vector<vector<int>>& clips, int time) {
sort(clips.begin(), clips.end());
int res = 0, curr_end = 0, next_end = 0, i = 0, n = clips.size();
while (curr_end < time) {
while (i < n && clips[i][0] <= curr_end) {
next_end = max(next_end, clips[i][1]);
i++;
}
if (curr_end == next_end) return -1;
res++;
curr_end = next_end;
}
return res;
}
};
import java.util.*;
class Solution {
public int videoStitching(int[][] clips, int time) {
Arrays.sort(clips, (a, b) -> a[0] - b[0]);
int res = 0, curr_end = 0, next_end = 0, i = 0, n = clips.length;
while (curr_end < time) {
while (i < n && clips[i][0] <= curr_end) {
next_end = Math.max(next_end, clips[i][1]);
i++;
}
if (curr_end == next_end) return -1;
res++;
curr_end = next_end;
}
return res;
}
}
var videoStitching = function(clips, time) {
clips.sort((a, b) => a[0] - b[0]);
let res = 0, curr_end = 0, next_end = 0, i = 0, n = clips.length;
while (curr_end < time) {
while (i < n && clips[i][0] <= curr_end) {
next_end = Math.max(next_end, clips[i][1]);
i++;
}
if (curr_end === next_end) return -1;
res++;
curr_end = next_end;
}
return res;
};
[start, end], where start and end are integers indicating the start and end times (in seconds) of that clip. You are also given an integer time representing the total duration of the event you want to cover (from 0 to time seconds, inclusive of 0 but exclusive of time).[0, time) entirely. If it is impossible to cover the entire interval, return -1.[start, end] with 0 <= start < end <= 100.clips array by their start time. This allows us to process them in order and makes it easier to find which clips can extend the current coverage.curr_end: The end of the interval we've currently covered.next_end: The furthest point we can reach from the current position using available clips.res: The count of clips used so far.i: Index to iterate through the sorted clips.curr_end is less than time:
curr_end, update next_end to the maximum of their end values. This finds the best possible extension from the current coverage.next_end does not advance beyond curr_end, it means there is a gap and it's impossible to cover the interval. Return -1.res) and set curr_end to next_end.res as the minimum number of clips needed.clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], time = 10[[0,2],[1,5],[1,9],[4,6],[5,9],[8,10]]curr_end = 0, next_end = 0, res = 0, i = 0next_end = 2 (i=1).next_end > curr_end, increment res = 1, set curr_end = 2.next_end to 9 (max of 5 and 9). (i=3)res = 2, set curr_end = 9.next_end to 9 (no further extension here). (i=5)next_end to 10. (i=6)res = 3, set curr_end = 10.