Skip to content

Commit 5d9bad8

Browse files
committed
1024. 视频拼接
1 parent eb99bb3 commit 5d9bad8

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

dp/1024_videoStitching.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def videoStitching(self, clips: List[List[int]], T: int) -> int:
6+
dp = [0] + [float("inf")] * T
7+
for i in range(1, T + 1):
8+
for aj, bj in clips:
9+
if aj < i <= bj:
10+
dp[i] = min(dp[i], dp[aj] + 1)
11+
12+
return -1 if dp[T] == float("inf") else dp[T]
13+
14+
def videoStitching_greed(self, clips: List[List[int]], T: int) -> int:
15+
maxn = [0] * T
16+
last = ret = pre = 0
17+
for a, b in clips:
18+
if a < T:
19+
maxn[a] = max(maxn[a], b)
20+
21+
for i in range(T):
22+
last = max(last, maxn[i])
23+
if i == last:
24+
return -1
25+
if i == pre:
26+
ret += 1
27+
pre = last
28+
29+
return ret
30+
31+
32+
if __name__ == '__main__':
33+
clips = [[0, 2], [4, 6], [8, 10], [1, 9], [1, 5], [5, 9]]
34+
T = 10
35+
solution = Solution()
36+
print(solution.videoStitching_greed(clips, T))

0 commit comments

Comments
 (0)