-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1024.py
53 lines (43 loc) · 1.81 KB
/
1024.py
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
class Solution:
def videoStitching(self, clips: List[List[int]], T: int) -> int:
the_map = [[None for i in range(T+1)] for j in range(T+1)]
return self.least_clips(clips, the_map, 0, T)
def least_clips(self, clips, the_map, start, stop):
if stop < start:
return 0
temp_min = -1
for c in clips:
if the_map[start][stop] is not None:
return the_map[start][stop]
if c[0] <= start and c[1] >= stop:
the_map[start][stop] = 1
return 1
elif c[0] > stop or c[1] < start or c[1] <= c[0]:
continue
elif c[0] <= start < c[1]:
left_res = self.least_clips(clips, the_map, c[1], stop)
if left_res < 0:
continue
if temp_min < 0:
temp_min = 1 + left_res
else:
temp_min = min(temp_min, 1 + left_res)
elif c[0] < stop <= c[1]:
left_res = self.least_clips(clips, the_map, start, c[0])
if left_res < 0:
continue
if temp_min < 0:
temp_min = 1 + left_res
else:
temp_min = min(temp_min, 1 + left_res)
elif c[0] >= start and c[1] <= stop:
left_res_l = self.least_clips(clips, the_map, start, c[0])
left_res_r = self.least_clips(clips, the_map, c[1]+1, stop)
if left_res_l < 0 or left_res_r < 0:
continue
if temp_min < 0:
temp_min = 1 + left_res_l + left_res_r
else:
temp_min = min(temp_min, 1 + left_res_l + left_res_r)
the_map[start][stop] = temp_min
return temp_min