1024. 视频拼接
你将会获得一系列视频片段,这些片段来自于一项持续时长为 time 秒的体育赛事。这些片段可能有所重叠,也可能长度不一。
使用数组 clips 描述所有的视频片段,其中 clips[i] = [starti, endi] 表示:某个视频片段开始于 starti 并于 endi 结束。
甚至可以对这些片段自由地再剪辑:
例如,片段 [0, 7] 可以剪切成 [0, 1] + [1, 3] + [3, 7] 三部分。
我们需要将这些片段进行再剪辑,并将剪辑后的内容拼接成覆盖整个运动过程的片段([0, time])。返回所需片段的最小数目,如果无法完成该任务,则返回 -1 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/video-stitching
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
跳一跳
import java.util.Arrays;
import java.util.Comparator;
class Solution {
public int videoStitching(int[][] clips, int time) {
if (clips == null || clips.length == 0) {
return -1;
}
Arrays.sort(clips, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[0], o2[0]);
}
});
int ans = 0;
int cur = -1, next = 0;
for (int[] clip : clips) {
if (cur < clip[0]) {
if (next < clip[0]) {
return -1;
}
cur = next;
ans++;
}
next = Math.max(next, clip[1]);
if (next >= time) {
break;
}
}
return next >= time ? ans : -1;
}
}
优先队列
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
class Solution {
public int videoStitching(int[][] clips, int time) {
if (clips == null || clips.length == 0) {
return -1;
}
Arrays.sort(clips, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[0], o2[0]);
}
});
int ret = 0;
int curPos = 0;
PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return Integer.compare(o2[1], o1[1]);
}
});
int idx = 0;
while (idx < clips.length) {
while (idx < clips.length && clips[idx][0] <= curPos) {
queue.offer(clips[idx++]);
}
if (queue.isEmpty()) {
return -1;
}
int[] best = queue.poll();
if (best[1] > curPos) {
curPos = best[1];
ret++;
if (curPos >= time) {
return ret;
}
} else {
return -1;
}
}
return -1;
}
}
动态规划
import java.util.Arrays;
class Solution {
public int videoStitching(int[][] clips, int time) {
if (clips == null || clips.length == 0) {
return -1;
}
int[] dp = new int[time + 1];
Arrays.fill(dp, Integer.MAX_VALUE - 1);
dp[0] = 0;
for (int i = 1; i <= time; ++i) {
for (int[] clip : clips) {
if (clip[0] < i && i <= clip[1]) {
dp[i] = Math.min(dp[i], dp[clip[0]] + 1);
}
}
}
return dp[time] == Integer.MAX_VALUE - 1 ? -1 : dp[time];
}
}
贪心
import java.util.Arrays;
class Solution {
public int videoStitching(int[][] clips, int time) {
if (clips == null || clips.length == 0) {
return -1;
}
int[] maxDistance = new int[time];
for (int[] clip : clips) {
if (clip[0] < time) {
maxDistance[clip[0]] = Math.max(maxDistance[clip[0]], clip[1]);
}
}
int ret = 0, pre = 0, last = 0;
for (int i = 0; i < time; ++i) {
last = Math.max(last, maxDistance[i]);
if (last == i) {
return -1;
}
if (i == pre) {
ret++;
pre = last;
}
}
return ret;
}
}
贪心(转化为跳一跳)
import java.util.Arrays;
class Solution {
public int videoStitching(int[][] clips, int time) {
if (clips == null || clips.length == 0) {
return -1;
}
int[] maxDistance = new int[time + 1];
for (int[] clip : clips) {
if (clip[0] <= time) {
maxDistance[clip[0]] = Math.max(maxDistance[clip[0]], clip[1]);
}
}
int ret = 0, curEnd = -1, nextEnd = 0;
for (int i = 0; i <= time; ++i) {
if (i > curEnd) {
if (i > nextEnd) {
return -1;
}
ret++;
curEnd = nextEnd;
}
nextEnd = Math.max(nextEnd, maxDistance[i]);
if (nextEnd >= time) {
return ret;
}
}
return -1;
}
}
心之所向,素履以往 生如逆旅,一苇以航
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!