Skip to content

Commit 9fe71b3

Browse files
committed
feat: add solutions to lc problem: No.1024
No.1024.Video Stitching
1 parent aea3072 commit 9fe71b3

File tree

6 files changed

+299
-18
lines changed

6 files changed

+299
-18
lines changed

solution/1000-1099/1024.Video Stitching/README.md

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,137 @@
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

67+
**方法一:贪心**
68+
69+
注意到,如果相同起点的子区间有多个,那么选择右端点最大的那个子区间是最优的。
70+
71+
因此,我们可以预处理所有子区间,对于每一个位置 $i$,算出所有以 $i$ 为起点的子区间中,右端点最大的那个位置,记录在数组 $last[i]$ 中。
72+
73+
我们定义变量 `mx` 表示当前能够到达的最远位置,变量 `ans` 表示当前需要的最少子区间数,变量 `pre` 表示上一个被使用的子区间的右端点。
74+
75+
接下来,我们从 $0$ 开始枚举所有位置 $i$,用 $last[i]$ 来更新 `mx`。如果更新后 $mx = i$,说明无法覆盖下一个位置,因此无法完成任务,返回 $-1$。
76+
77+
同时我们记录上一个被使用的子区间的右端点 `pre`,如果 $pre = i$,说明需要使用一个新的子区间,因此我们将 `ans` 加 $1$,并将 `pre` 更新为 `mx`
78+
79+
遍历结束后,返回 `ans` 即可。
80+
81+
时间复杂度 $O(n+m)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别是数组 `clips` 的长度和 `time` 的值。
82+
6783
<!-- tabs:start -->
6884

6985
### **Python3**
7086

7187
<!-- 这里可写当前语言的特殊实现逻辑 -->
7288

7389
```python
74-
90+
class Solution:
91+
def videoStitching(self, clips: List[List[int]], time: int) -> int:
92+
last = [0] * time
93+
for a, b in clips:
94+
if a < time:
95+
last[a] = max(last[a], b)
96+
ans = mx = pre = 0
97+
for i, v in enumerate(last):
98+
mx = max(mx, v)
99+
if mx <= i:
100+
return -1
101+
if pre == i:
102+
ans += 1
103+
pre = mx
104+
return ans
75105
```
76106

77107
### **Java**
78108

79109
<!-- 这里可写当前语言的特殊实现逻辑 -->
80110

81111
```java
112+
class Solution {
113+
public int videoStitching(int[][] clips, int time) {
114+
int[] last = new int[time];
115+
for (var e : clips) {
116+
int a = e[0], b = e[1];
117+
if (a < time) {
118+
last[a] = Math.max(last[a], b);
119+
}
120+
}
121+
int ans = 0, mx = 0, pre = 0;
122+
for (int i = 0; i < time; ++i) {
123+
mx = Math.max(mx, last[i]);
124+
if (mx <= i) {
125+
return -1;
126+
}
127+
if (pre == i) {
128+
++ans;
129+
pre = mx;
130+
}
131+
}
132+
return ans;
133+
}
134+
}
135+
```
136+
137+
### **C++**
138+
139+
```cpp
140+
class Solution {
141+
public:
142+
int videoStitching(vector<vector<int>>& clips, int time) {
143+
vector<int> last(time);
144+
for (auto& v : clips) {
145+
int a = v[0], b = v[1];
146+
if (a < time) {
147+
last[a] = max(last[a], b);
148+
}
149+
}
150+
int mx = 0, ans = 0;
151+
int pre = 0;
152+
for (int i = 0; i < time; ++i) {
153+
mx = max(mx, last[i]);
154+
if (mx <= i) {
155+
return -1;
156+
}
157+
if (pre == i) {
158+
++ans;
159+
pre = mx;
160+
}
161+
}
162+
return ans;
163+
}
164+
};
165+
```
82166
167+
### **Go**
168+
169+
```go
170+
func videoStitching(clips [][]int, time int) int {
171+
last := make([]int, time)
172+
for _, v := range clips {
173+
a, b := v[0], v[1]
174+
if a < time {
175+
last[a] = max(last[a], b)
176+
}
177+
}
178+
ans, mx, pre := 0, 0, 0
179+
for i, v := range last {
180+
mx = max(mx, v)
181+
if mx <= i {
182+
return -1
183+
}
184+
if pre == i {
185+
ans++
186+
pre = mx
187+
}
188+
}
189+
return ans
190+
}
191+
192+
func max(a, b int) int {
193+
if a > b {
194+
return a
195+
}
196+
return b
197+
}
83198
```
84199

85200
### **...**

solution/1000-1099/1024.Video Stitching/README_EN.md

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,112 @@ Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 1
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def videoStitching(self, clips: List[List[int]], time: int) -> int:
65+
last = [0] * time
66+
for a, b in clips:
67+
if a < time:
68+
last[a] = max(last[a], b)
69+
ans = mx = pre = 0
70+
for i, v in enumerate(last):
71+
mx = max(mx, v)
72+
if mx <= i:
73+
return -1
74+
if pre == i:
75+
ans += 1
76+
pre = mx
77+
return ans
6478
```
6579

6680
### **Java**
6781

6882
```java
83+
class Solution {
84+
public int videoStitching(int[][] clips, int time) {
85+
int[] last = new int[time];
86+
for (var e : clips) {
87+
int a = e[0], b = e[1];
88+
if (a < time) {
89+
last[a] = Math.max(last[a], b);
90+
}
91+
}
92+
int ans = 0, mx = 0, pre = 0;
93+
for (int i = 0; i < time; ++i) {
94+
mx = Math.max(mx, last[i]);
95+
if (mx <= i) {
96+
return -1;
97+
}
98+
if (pre == i) {
99+
++ans;
100+
pre = mx;
101+
}
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
int videoStitching(vector<vector<int>>& clips, int time) {
114+
vector<int> last(time);
115+
for (auto& v : clips) {
116+
int a = v[0], b = v[1];
117+
if (a < time) {
118+
last[a] = max(last[a], b);
119+
}
120+
}
121+
int mx = 0, ans = 0;
122+
int pre = 0;
123+
for (int i = 0; i < time; ++i) {
124+
mx = max(mx, last[i]);
125+
if (mx <= i) {
126+
return -1;
127+
}
128+
if (pre == i) {
129+
++ans;
130+
pre = mx;
131+
}
132+
}
133+
return ans;
134+
}
135+
};
136+
```
69137
138+
### **Go**
139+
140+
```go
141+
func videoStitching(clips [][]int, time int) int {
142+
last := make([]int, time)
143+
for _, v := range clips {
144+
a, b := v[0], v[1]
145+
if a < time {
146+
last[a] = max(last[a], b)
147+
}
148+
}
149+
ans, mx, pre := 0, 0, 0
150+
for i, v := range last {
151+
mx = max(mx, v)
152+
if mx <= i {
153+
return -1
154+
}
155+
if pre == i {
156+
ans++
157+
pre = mx
158+
}
159+
}
160+
return ans
161+
}
162+
163+
func max(a, b int) int {
164+
if a > b {
165+
return a
166+
}
167+
return b
168+
}
70169
```
71170

72171
### **...**
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
class Solution {
22
public:
3-
int videoStitching(vector<vector<int>>& clips, int T) {
4-
int maxEnding = 0;
5-
int count = 0;
6-
vector<int> maxEnd(T + 1, 0); // maxEnd[i] : maximum ending time of all videos that start by time i
7-
for (int i = 0; i < clips.size(); ++i) {
8-
if (clips[i][0] >= T) continue;
9-
maxEnd[clips[i][0]] = max(maxEnd[clips[i][0]], clips[i][1]);
10-
}
11-
for (int i = 1; i < T; ++i) {
12-
maxEnd[i] = max(maxEnd[i], maxEnd[i - 1]);
3+
int videoStitching(vector<vector<int>>& clips, int time) {
4+
vector<int> last(time);
5+
for (auto& v : clips) {
6+
int a = v[0], b = v[1];
7+
if (a < time) {
8+
last[a] = max(last[a], b);
9+
}
1310
}
14-
for (int i = 0; i < T; ++i) {
15-
if (maxEnding == i) { // select video with maximum ending if one more video is necessary
16-
count++;
17-
maxEnding = maxEnd[i];
18-
} else if (maxEnding < i) {
11+
int mx = 0, ans = 0;
12+
int pre = 0;
13+
for (int i = 0; i < time; ++i) {
14+
mx = max(mx, last[i]);
15+
if (mx <= i) {
1916
return -1;
2017
}
18+
if (pre == i) {
19+
++ans;
20+
pre = mx;
21+
}
2122
}
22-
return count;
23+
return ans;
2324
}
2425
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func videoStitching(clips [][]int, time int) int {
2+
last := make([]int, time)
3+
for _, v := range clips {
4+
a, b := v[0], v[1]
5+
if a < time {
6+
last[a] = max(last[a], b)
7+
}
8+
}
9+
ans, mx, pre := 0, 0, 0
10+
for i, v := range last {
11+
mx = max(mx, v)
12+
if mx <= i {
13+
return -1
14+
}
15+
if pre == i {
16+
ans++
17+
pre = mx
18+
}
19+
}
20+
return ans
21+
}
22+
23+
func max(a, b int) int {
24+
if a > b {
25+
return a
26+
}
27+
return b
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int videoStitching(int[][] clips, int time) {
3+
int[] last = new int[time];
4+
for (var e : clips) {
5+
int a = e[0], b = e[1];
6+
if (a < time) {
7+
last[a] = Math.max(last[a], b);
8+
}
9+
}
10+
int ans = 0, mx = 0, pre = 0;
11+
for (int i = 0; i < time; ++i) {
12+
mx = Math.max(mx, last[i]);
13+
if (mx <= i) {
14+
return -1;
15+
}
16+
if (pre == i) {
17+
++ans;
18+
pre = mx;
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def videoStitching(self, clips: List[List[int]], time: int) -> int:
3+
last = [0] * time
4+
for a, b in clips:
5+
if a < time:
6+
last[a] = max(last[a], b)
7+
ans = mx = pre = 0
8+
for i, v in enumerate(last):
9+
mx = max(mx, v)
10+
if mx <= i:
11+
return -1
12+
if pre == i:
13+
ans += 1
14+
pre = mx
15+
return ans

0 commit comments

Comments
 (0)