-
Notifications
You must be signed in to change notification settings - Fork 225
/
1024-video-stitching.js
97 lines (85 loc) · 1.97 KB
/
1024-video-stitching.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @param {number[][]} clips
* @param {number} time
* @return {number}
*/
const videoStitching = function(clips, time) {
const n = clips.length
if(time === 0) return 0
clips.sort((a, b) => a[0] === b[0] ? b[1] - a[1] : a[0] - b[0])
let res = 0, start = 0, end = 0, nextEnd = 0, idx = 0
while(idx < n) {
nextEnd = end
while(idx < n && clips[idx][0] <= end) {
nextEnd = Math.max(nextEnd, clips[idx][1])
idx++
}
res++
if(nextEnd >= time) return res
else if(nextEnd === end) return -1
else {
end = nextEnd
}
}
return -1
};
// anonther
/**
* @param {number[][]} clips
* @param {number} T
* @return {number}
*/
const videoStitching = function (clips, T) {
clips.sort((a, b) => a[0] - b[0])
if(T === 0) return 0
let laststart = -1,
curend = 0,
count = 0
for (let i = 0; i < clips.length; ) {
if (clips[i][0] > curend) return -1
let maxend = curend
// while one clip's start is before or equal to current end
while (i < clips.length && clips[i][0] <= curend) {
maxend = Math.max(maxend, clips[i][1])
i++
}
count++
curend = maxend
if (curend >= T) return count
}
return -1
}
// another
/**
* @param {number[][]} clips
* @param {number} T
* @return {number}
*/
const videoStitching = function (clips, T) {
clips.sort((a, b) => a[0] - b[0])
let res = 0
for(let i = 0, start = 0, end = 0, len = clips.length; start < T; start = end, res++) {
for(; i < len && clips[i][0] <= start; i++) {
end = Math.max(end, clips[i][1])
}
if(start === end) return -1
}
return res
}
// another
/**
* @param {number[][]} clips
* @param {number} T
* @return {number}
*/
const videoStitching = function (clips, T) {
const dp = Array(T + 1).fill( T + 1 )
dp[0] = 0
for(let i = 0; i <= T; i++) {
for(let c of clips) {
if(i >= c[0] && i <= c[1]) dp[i] = Math.min(dp[i], dp[c[0]] + 1)
}
if(dp[i] === T + 1) return -1
}
return dp[T]
}