Contest 131 - Prob 4 Video Stitc
2019-04-07 本文已影响0人
人树杨
- This problem can be solved by a greedy algorithm.
-
res
represents the number of steps;current
represents the farthest time we could reach withres
steps. In the while loop, go through all the clips and find all the clips which cross the timecurrent
, because these clips can be used to extendcurrent
. - If
current
reachesT
, return the number of stepsres
. If such clips do not exist, thencurrent
cannot be extended any more, and we should return-1
.
class Solution:
def videoStitching(self, clips: List[List[int]], T: int) -> int:
res, current = 0, 0
while True:
current = max([j for i, j in clips if i <= current and j > current], default=0)
if current == 0: return -1
if current >= T: return res + 1
res += 1