【刷题打卡】1024. 视频拼接

165 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

一、题目描述:

1024. 视频拼接 - 力扣(LeetCode) (leetcode-cn.com)

你将会获得一系列视频片段,这些片段来自于一项持续时长为 time 秒的体育赛事。这些片段可能有所重叠,也可能长度不一。

使用数组 clips 描述所有的视频片段,其中 clips[i] = [starti, endi] 表示:某个视频片段开始于 starti 并于 endi 结束。

甚至可以对这些片段自由地再剪辑:

  • 例如,片段 [0, 7] 可以剪切成 [0, 1] + [1, 3] + [3, 7] 三部分。 我们需要将这些片段进行再剪辑,并将剪辑后的内容拼接成覆盖整个运动过程的片段([0, time])。返回所需片段的最小数目,如果无法完成该任务,则返回 -1 。

示例 1:

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

示例 2:

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

示例 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]], time = 9
输出:3
解释: 
选取片段 [0,4], [4,7][6,9]

提示:

  • 1 <= clips.length <= 100
  • 0 <= starti <= endi <= 100
  • 1 <= time <= 100

二、思路分析:

  1. 如果T的时间为0,则直接返回0,第一次提交没有想到这个,导致最后一个用例不通过;
  2. 要能到达时间T的完整视频片段,必须从0开始,首先对clips进行排序(按照startTime升序);
  3. 找到第一个开始时间不为0的片段,并记录为0片段的最大值maxZeroRight;
  4. 广度优先,第一次找的值的范围是视频的开始时间startTime > 0 && startTime <= maxZeroRight,将满足条件的视频片段增加到queue;
  5. 后面的循环按照4中的片段时间确定下一候选集合
  6. 如果结束时间>=T,返回步数就可以;
  7. 结束BFS还找不到,则返回-1;

三、AC 代码:

class Solution {
    public int videoStitching(int[][] clips, int T) {
        if (T == 0) {
            return 0;
        }
        Arrays.sort(clips, (o1, o2) -> o1[0] - o2[0]);
        int[] maxStartRightValueAndStartIndex = findMaxStartRightValueAndStartIndex(clips);
        int startIndex = maxStartRightValueAndStartIndex[1];
        int maxZeroRight = maxStartRightValueAndStartIndex[0];
        int step = 0;
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{0, maxZeroRight});
        boolean[] visited = new boolean[clips.length];
        while (!queue.isEmpty()) {
            step++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int[] compare = queue.poll();
                assert compare != null;
                if (compare[1] >= T) {
                    return step;
                }
                startIndex = updateQueueValue(startIndex, queue, visited, clips, compare);
            }
        }
        return -1;
    }

    private int[] findMaxStartRightValueAndStartIndex(int[][] clips) {
        int startIndex = 0;
        int maxZeroRight = Integer.MIN_VALUE;
        for (int i = 0; i < clips.length; i++) {
            if (clips[i][0] == 0) {
                maxZeroRight = Math.max(maxZeroRight, clips[i][1]);
            } else {
                startIndex = i;
                break;
            }
        }
        return new int[]{maxZeroRight, startIndex};
    }

    private int updateQueueValue(int startIndex, Queue<int[]> queue, boolean[] visited, int[][] clips, int[] compare) {
        int nextStartIndex = 0;
        for (int j = startIndex; j < clips.length; j++) {
            if (visited[j]) {
                continue;
            }
            if (clips[j][0] <= compare[1]) {
                queue.add(new int[]{clips[j][0], clips[j][1]});
                visited[j] = true;
            } else {
                nextStartIndex = j;
                break;
            }
        }
        return nextStartIndex;
    }
}