当前位置:网站首页>Leetcode daily question (1024. video sticking)
Leetcode daily question (1024. video sticking)
2022-07-03 09:33:00 【wangjun861205】
You are given a series of video clips from a sporting event that lasted time seconds. These video clips can be overlapping with each other and have varying lengths.
Each video clip is described by an array clips where clips[i] = [starti, endi] indicates that the ith clip started at starti and ended at endi.
We can cut these clips into segments freely.
For example, a clip [0, 7] can be cut into segments [0, 1] + [1, 3] + [3, 7].
Return the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event [0, time]. If the task is impossible, return -1.
Example 1:
Input: clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], time = 10
Output: 3
Explanation: We take the clips [0,2], [8,10], [1,9]; a total of 3 clips.
Then, we can reconstruct the sporting event as follows:
We cut [1,9] into segments [1,2] + [2,8] + [8,9].
Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 10].
Example 2:
Input: clips = [[0,1],[1,2]], time = 5
Output: -1
Explanation: We cannot cover [0,5] with only [0,1] and [1,2].
Example 3:
Input: clips = [[0,1],[6,8],[0,2],[5,6],[0,4],[0,3],[6,7],[1,3],[4,7],[1,4],[2,5],[2,6],[3,4],[4,5],[5,7],[6,9]], time = 9
Output: 3
Explanation: We can take clips [0,4], [4,7], and [6,9].
Constraints:
- 1 <= clips.length <= 100
- 0 <= starti <= endi <= 100
- 1 <= time <= 100
First sort the clips by start time , If the start time is the same, sort by the end time in reverse , Then maintain a stack , Save the pieces that need to be spliced , Traverse the ordered fragments , Consider the following situations :
- The end time of the current clip is less than or equal to the end time of the top of the stack , The current clip is not stacked
- The end time of the current clip is greater than the end time at the top of the stack , The start time is greater than the top of the stack -1 The end time of , The current segment is normally stacked
- The end time of the current clip is greater than the end time at the top of the stack , The start time is less than or equal to the top of the stack -1 The end time of , Bomb stack , Until this condition is not met , Stack current clip
Other details should be noted that the segment must have a start time of 0 And there are fragments with a certain time span , Otherwise, it is impossible to complete the splicing , During traversal , If the end time at the top of the stack is greater than or equal to time Then there is no need to continue traversal
impl Solution {
pub fn video_stitching(mut clips: Vec<Vec<i32>>, time: i32) -> i32 {
clips.sort_by(|a, b| {
if a[0] == b[0] {
return b[1].cmp(&a[1]);
}
a[0].cmp(&b[0])
});
if clips.first().unwrap()[0] != 0 {
return -1;
}
let mut stack: Vec<Vec<i32>> = Vec::new();
'outer: for c in clips {
while let Some(last) = stack.pop() {
if last[1] >= time {
stack.push(last);
break 'outer;
}
if c[0] > last[1] || c[1] <= last[1] {
stack.push(last);
continue 'outer;
}
if stack.is_empty() {
stack.push(last);
stack.push(c);
continue 'outer;
}
if stack.last().unwrap()[1] >= c[0] {
continue;
}
stack.push(last);
break;
}
stack.push(c);
continue;
}
if stack.last().unwrap()[1] < time {
return -1;
}
stack.len() as i32
}
}
边栏推荐
- Trial of the combination of RDS and crawler
- 解决Editor.md上传图片获取不到图片地址问题
- [point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
- The idea of compiling VBA Encyclopedia
- Powerdesign reverse wizard such as SQL and generates name and comment
- Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
- Using Hudi in idea
- The rise and fall of mobile phones in my perspective these 10 years
- Django operates Excel files through openpyxl to import data into the database in batches.
- Windows安装Redis详细步骤
猜你喜欢

Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
![[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks](/img/61/aa8d0067868ce9e28cadf5369cd65e.png)
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks

Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)

小王叔叔的博客目录【持续更新中】
![[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature](/img/66/2e7668cfed1ef4ddad26deed44a33a.png)
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature

Crawler career from scratch (IV): climb the bullet curtain of station B through API
![[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion](/img/76/b92fe4549cacba15c113993a07abb8.png)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion

Go language - JSON processing

Redis learning (I)

Spark structured stream writing Hudi practice
随机推荐
Jenkins learning (III) -- setting scheduled tasks
Hudi quick experience (including detailed operation steps and screenshots)
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
DSP data calculation error
Analysis of the implementation principle of an open source markdown to rich text editor
数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
Overview of database system
LeetCode每日一题(2232. Minimize Result by Adding Parentheses to Expression)
PolyWorks script development learning notes (II) -treeview basic operations
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
制作jetson nano最基本的根文件系统、服务器挂载NFS文件系统
Jetson nano custom boot icon kernel logo CBOOT logo
LeetCode每日一题(1024. Video Stitching)
Beego learning - Tencent cloud upload pictures
Flink学习笔记(十一)Table API 和 SQL
小王叔叔的博客目录【持续更新中】
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
[kotlin learning] classes, objects and interfaces - define class inheritance structure
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
Spark 结构化流写入Hudi 实践