leetcode.1024. Video stitching

1024. Video stitching

You will get a series of video clips from a sporting event that lasts T seconds. These fragments may overlap, or they may vary in length.

Video clips clips[i]can be expressed in range: starts clips[i][0]and at clips[i][1]the end. You may even fragments of these clips consisting, for example, a fragment [0, 7]can be cut into [0, 1] + [1, 3] + [3, 7]three parts.

We need to re-edit these segments and stitch the edited content into segments that cover the entire movement process ([0, T]). Return the minimum number of required fragments, if the task cannot be completed, return -1.

Example 1:

输入:clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], T = 10
输出:3
解释:
我们选中 [0,2], [8,10], [1,9] 这三个片段。
然后,按下面的方案重制比赛片段:
将 [1,9] 再剪辑为 [1,2] + [2,8] + [8,9] 。
现在我们手上有 [0,2] + [2,8] + [8,10],而这些涵盖了整场比赛 [0, 10]。

Example 2:

输入:clips = [[0,1],[1,2]], T = 5
输出:-1
解释:
我们无法只用 [0,1] 和 [1,2] 覆盖 [0,5] 的整个过程。

Example 3:

输入:clips = [[0,1],[6,8],[0,2],[5,6],[0,4],[0,3],[6,7],[1,3],[4,7],[1,4],[2,5],[2,6],[3,4],[4,5],[5,7],[6,9]], T = 9
输出:3
解释: 
我们选取片段 [0,4], [4,7] 和 [6,9] 。

Example 4:

输入:clips = [[0,4],[2,8]], T = 5
输出:2
解释:
注意,你可能录制超过比赛结束时间的视频。

prompt:

1 <= clips.length <= 100
0 <= clips[i][0] <= clips[i][1] <= 100
0 <= T <= 100

Dynamic programming

It should be noted that
1. Confirm the original problem and the sub-problem. The
original problem is the minimum number of fragments required for T seconds. The sub-problems are the minimum number of fragments required in the 0th second, the minimum number of fragments required in the first second, the minimum number of fragments required in the second second, and the minimum number of fragments required in the third second
. 2. Confirm the state
of the dynamic programming of this question is single, The i state is the minimum number of fragments in i seconds
3. Confirm the value of the
boundary state The boundary state is the 0th second, and 0 solutions are needed
4. Determine the state transition equation
The i state, dp[i] = Math.min(dp[i], dp[clip[0]] + 1);
code

    public int videoStitching(int[][] clips, int T) {
    
    
        int[] dp = new int[T + 1];
        Arrays.fill(dp, Integer.MAX_VALUE - 1);
        dp[0] = 0;
        for (int i = 1; i <= T; i++) {
    
    
            for (int[] clip : clips) {
    
    
                if (clip[0] < i && i <= clip[1]) {
    
    
                    dp[i] = Math.min(dp[i], dp[clip[0]] + 1);
                }
            }
        }
        return dp[T] == Integer.MAX_VALUE - 1 ? -1 : dp[T];
    }

Note that the int[] cliparray has only two elements, the fragment starts and the fragment ends. MAX_VALUEMinus one is to prevent MAX_VALUEoverflow , plus one. Also, the dp array here is to be initialized, first set all to the maximum integer value minus one, and then initialize dp[0].
Insert picture description here

how are you

The greedy algorithm is to traverse once and find the farthest one that can be jumped to at the same position.
Insert picture description here
The above three lines have the same abscissa position, we find the first one is the longest, choose it.
The next step can be parameter 55. The official solution of the jumping game , we keep traversing T until we can't jump (the maximum jumping point was recorded last time), and then add 1. It should be noted that 当前元素 == 本区间最大元素(无法到达后续位置)there is no way to jump, as shown below.
Insert picture description here

class Solution {
    
    
    public int videoStitching(int[][] clips, int T) {
    
    
        if (clips == null) {
    
    
            return 0;
        }

        int[] maxEnd = new int[T];  // 用于保存 以当前数字(下标)为起点 的区间的 最大的结束位置

        /*
            遍历clips,初始化maxEnd数组(每个元素开头的区间的最大结束位置)
         */
        for (int[] clip : clips) {
    
    
            if (clip[0] < T) {
    
    
                maxEnd[clip[0]] = Math.max(maxEnd[clip[0]], clip[1]);
            }
        }

        /*
            根据maxEnd数组,计算最终结果
                因为maxEnd[i]数组为最大结束位置,因此:
                    1、当前元素 == 本区间最大元素,
                        即:区间断开,无法连续到后续位置,返回-1
                    2、当前元素 == 上一个区间的最大结束元素,
                        即:到达了上一个满足条件的区间的结束位置
                        这时的last为当前最大的结束位置,我们将其放入满足条件的区间集合之中
                        (因为本题只需要我们记录 满足条件的区间个数,因为只需要 更新count和pre 即可)
         */
        int pre = 0;    // 记录 结果中上一次的最大结束位置(本轮的最小开始位置)
        int last = 0;   // 记录当前遍历到的 区间最大结束位置
        int count = 0; // 记录结果
        for (int i = 0; i < T; i++) {
    
    
            last = Math.max(maxEnd[i], last);
            if (i == last) {
    
        // 当前元素 == 本区间最大元素(无法到达后续位置)
                return -1;
            }

            if (i == pre) {
    
     // 当前元素 == 上一个区间的最大元素
                count++;
                pre = last;
            }
        }
        return count;
    }
}

reference

Official, dynamic and
greedy solutions

Guess you like

Origin blog.csdn.net/e891377/article/details/109261761