贪心算法,每次选择开始在上一段视频结束之前的视频片段
使选择的视频结束时间最晚。这样贪心选择的结果最好
def videoStitching(self, clips, time):
"""
:type clips: List[List[int]]
:type time: int
:rtype: int
"""
clips.sort() #按照起点排序
i = 0
res = 0
n = len(clips)
curend,nextend = 0,0
while i < n and clips[i][0] <= curend: #起点要小于等于当前的结尾
while i < n and clips[i][0] <= curend: #贪心选择结尾最靠后的那个片段
nextend = max(clips[i][1],nextend)
i += 1
res += 1
curend = nextend
if curend >= time:
return res
return -1