当前位置:网站首页>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
}
}
边栏推荐
- Jenkins learning (II) -- setting up Chinese
- Spark cluster installation and deployment
- Failed building wheel for argon2 cffi when installing Jupiter
- LeetCode每日一题(2212. Maximum Points in an Archery Competition)
- [set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
- Run flash demo on ECS
- Equality judgment of long type
- 小王叔叔的博客目录【持续更新中】
- Detailed steps of windows installation redis
- [kotlin learning] classes, objects and interfaces - define class inheritance structure
猜你喜欢
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
Jenkins learning (III) -- setting scheduled tasks
解决Editor.md上传图片获取不到图片地址问题
PolyWorks script development learning notes (III) -treeview advanced operation
Hudi quick experience (including detailed operation steps and screenshots)
Principles of computer composition - cache, connection mapping, learning experience
Crawler career from scratch (IV): climb the bullet curtain of station B through API
Solve editor MD uploads pictures and cannot get the picture address
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
随机推荐
Quickly use markdown to edit articles
Desktop icon recognition based on OpenCV
专利查询网站
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Windows安装Redis详细步骤
Patent inquiry website
Powerdesign reverse wizard such as SQL and generates name and comment
Spark cluster installation and deployment
Leetcode daily question (745. prefix and suffix search)
一款开源的Markdown转富文本编辑器的实现原理剖析
Spark 集群安装与部署
Serializer rewrite: update and create methods
LeetCode每日一题(1362. Closest Divisors)
Flink学习笔记(十一)Table API 和 SQL
1922. Count Good Numbers
LeetCode每日一题(1856. Maximum Subarray Min-Product)
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
Install database -linux-5.7
Go language - IO project
Long类型的相等判断