LeetCode 1024. Video Stitching

given a 2D array and a time T
clips[i][0] represents for the starting time of clip i
clips[i][1] represents for the ending time of clip i

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.

idea:
at first i didn’t really understand the meaning of:

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].

but after take a look at the examples, i understand what that means:

as long as we all the segment covered from [0, T], it doesn’t matters if they are overlapped or not.

I’m thinking that at least we should sort it first.
what about next?
we do full search and get all the possible combinations that is valid and get the shortest one.
and no idea that what should I do.

according to the solution, the problem is relatively easy. and the explaination is pretty clear.
but there is a very tricky point in the following code which makes the whole code fail: becasue we have to get the min, so we initialize dp with MAX_VALUE, and it will cause the problem of overflow in here:

dp[c[0]] + 1

because we haven’t sort the input clips, so we can’t make sure that we will calculate dp[c[0]] first then dp[i], because we can’t be sure that c[0] is smaller than i

class Solution {
    public int videoStitching(int[][] clips, int T) {
        int[] dp = new int[T + 1];
        Arrays.fill(dp, Integer.MAX_VALUE); // 
        //dp[i] refers to if we only need to fill the time period of [0, i], the min num of intervals we need to use
        dp[0] = 0;
        for (int i = 0; i <= T; i++) { //for every momenet that is in the 0 to T
            for (int[] c: clips) {
                if (i >= c[0] && i <= c[1]) { //if current moment is in the intervals
                    dp[i] = Math.min(dp[i], dp[c[0]] + 1);
                }
            }//we need to check every clip to get this dp[i] which is minimum in all the dp[c[0]] + 1, and dp[c[0]] means the min num of intervals we need to use to fill the time period of [0, c[0]]
            if (dp[i] == Integer.MAX_VALUE) return -1; //that means for moment i, it's not with in any clips, so it's impossible for fill [0,T]
            
        }
        return dp[T];
    }
}

so the right code should be:

class Solution {
    public int videoStitching(int[][] clips, int T) {
        int[] dp = new int[T + 1];
        Arrays.fill(dp, T + 1); // 
        //dp[i] refers to if we only need to fill the time period of [0, i], the min num of intervals we need to use
        dp[0] = 0;
        for (int i = 0; i <= T; i++) { //for every momenet that is in the 0 to T
            for (int[] c: clips) {
                if (i >= c[0] && i <= c[1]) { //if current moment is in the intervals
                    dp[i] = Math.min(dp[i], dp[c[0]] + 1);
                }
            }//we need to check every clip to get this dp[i] which is minimum in all the dp[c[0]] + 1, and dp[c[0]] means the min num of intervals we need to use to fill the time period of [0, c[0]]
            if (dp[i] == T + 1) return -1; //that means for moment i, it's not with in any clips, so it's impossible for fill [0,T]
            
        }
        return dp[T];
    }
}
posted @ 2020-11-23 02:10  EvanMeetTheWorld  阅读(35)  评论(0)    收藏  举报