Contest 131 - Prob 4 Video Stitching

  • This problem can be solved by a greedy algorithm.
  • res represents the number of steps; current represents the farthest time we could reach with res steps. In the while loop, go through all the clips and find all the clips which cross the time current, because these clips can be used to extend current.
  • If current reaches T, return the number of steps res. If such clips do not exist, then current 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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容