[LeetCode]Video Stitching@Golang

Video Stitching
You are given a series of video clips from a sporting event that lasted T seconds. These video clips can be overlapping with each other and have varied lengths.

Each video clip clips[i] is an interval: it starts at time clips[i][0] and ends at time clips[i][1]. We can cut these clips into segments freely: for example, a clip [0, 7] can be cut into segments [0, 1] + [1, 3] + [3, 7].

Return the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event ([0, T]). If the task is impossible, return -1.

Example

Input: clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], T = 10
Output: 3
Explanation:
We take the clips [0,2], [8,10], [1,9]; a total of 3 clips.
Then, we can reconstruct the sporting event as follows:
We cut [1,9] into segments [1,2] + [2,8] + [8,9].
Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 10].

Solution

import "sort"
func videoStitching(clips [][]int, T int) int {
    sort.Slice(clips, func(i int, j int) bool{
        return clips[i][0]<clips[j][0]
    })
    
    ret:=0
    for start,end,i:=0, 0, 0;start<T;start = end {
        for i<len(clips) && clips[i][0]<=start {
            end = max(end, clips[i][1])
            i++
        }
        if start==end{
            return -1
        }
        ret ++
    }
    return ret
}

func max(a, b int)int {
    if a>b {
        return a
    }
    return b
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章