forked from everthis/leetcode-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1024-video-stitching.js
66 lines (59 loc) · 1.37 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
/**
* @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]
}