1.LeetCode1024題目鏈接
https://leetcode-cn.com/problems/video-stitching/
2.題目解析
該題只要是為了找到一些片段中能夠組成該視頻的最少的片段,首先我們需要找到最大或最小值俐巴,該題中乾吻,我們可以先找到包含最小值到片段,然后選出包含最小值的片段中最大到哪個位置锰蓬,然后找到包含這個位置的的片段中最大的位置,依次找到T,也就是視頻末尾杠愧。
public int videoStitching(int[][] clips, int T) {
//次數(shù)
int count = 0;
//每次需要的最大值
int max = 0;
while (max < T) {
//切片最大值
int slice = 0;
for (int[] c : clips) {
if (c[0] <= max && c[1] >= max) {
//多個包含去最大
slice = Math.max(slice, c[1]);
}
}
if (slice <= max) {
return -1;
}
max = slice;
++count;
}
return count;
}
image