LeetCode solutions

1081. Video Stitching

My accepted Python solution to LeetCode problem 1081, Video Stitching, running in 0ms.

  • Difficulty: Medium
  • Python
  • Runtime 0ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.4MB, accepted 2026-01-02.

python
class Solution:
    def videoStitching(self, clips: List[List[int]], time: int) -> int:
        # Sort by start time, then by end time (descending)
        clips.sort(key=lambda x: (x[0], -x[1]))
        
        count = 0
        current_end = 0
        i = 0
        n = len(clips)
        
        while current_end < time:
            # Find the clip that starts at or before current_end 
            # and extends the furthest
            max_end = current_end
            while i < n and clips[i][0] <= current_end:
                max_end = max(max_end, clips[i][1])
                i += 1
            
            if max_end == current_end:
                return -1  # Can't extend further
            
            count += 1
            current_end = max_end
        
        return count

Source