-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideoStitching.cpp
More file actions
27 lines (26 loc) · 961 Bytes
/
videoStitching.cpp
File metadata and controls
27 lines (26 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Source: https://leetcode.com/problems/video-stitching/
// Author: Miao Zhang
// Date: 2021-04-03
class Solution {
public:
int videoStitching(vector<vector<int>>& clips, int T) {
if (T == 0) return 0;
int kInf = 101;
vector<vector<int>> dp(T + 1, vector<int>(T + 1, kInf));
for (const auto& c: clips) {
int s = c[0];
int e = c[1];
for (int l = 1; l <= T; l++) {
for (int i = 0; i <= T - l; i++) {
int j = i + l;
if (s > j || e < i) continue;
if (s <= i && e >= j) dp[i][j] = 1;
else if (e >= j) dp[i][j] = min(dp[i][j], dp[i][s] + 1);
else if (s <= i) dp[i][j] = min(dp[i][j], dp[e][j] + 1);
else dp[i][j] = min(dp[i][j], dp[i][s] + 1 + dp[e][j]);
}
}
}
return dp[0][T] == kInf ? -1 : dp[0][T];
}
};