当前位置:网站首页>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
}
}
边栏推荐
- PolyWorks script development learning notes (III) -treeview advanced operation
- Directory and switching operation in file system
- [CSDN] C1 training problem analysis_ Part III_ JS Foundation
- LeetCode每日一题(2109. Adding Spaces to a String)
- Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
- Basic knowledge of database design
- PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
- LeetCode每日一题(968. Binary Tree Cameras)
- Flink学习笔记(十)Flink容错机制
- Notes on numerical analysis (II): numerical solution of linear equations
猜你喜欢

Construction of simple database learning environment
![[kotlin learning] classes, objects and interfaces - define class inheritance structure](/img/66/34396e51c59504ebbc6b6eb9831209.png)
[kotlin learning] classes, objects and interfaces - define class inheritance structure

Beego learning - Tencent cloud upload pictures

LeetCode每日一题(2090. K Radius Subarray Averages)

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

Hudi 数据管理和存储概述

Using Hudi in idea

Flask+supervisor installation realizes background process resident

Analysis of the implementation principle of an open source markdown to rich text editor

Modify idea code
随机推荐
全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
Win10安装ELK
[solution to the new version of Flink without bat startup file]
从0开始使用pnpm构建一个Monorepo方式管理的demo
Make the most basic root file system of Jetson nano and mount NFS file system on the server
Filter comments to filter out uncommented and default values
LeetCode每日一题(1024. Video Stitching)
Solve editor MD uploads pictures and cannot get the picture address
Hudi learning notes (III) analysis of core concepts
What do software test engineers do? Pass the technology to test whether there are loopholes in the software program
Flink学习笔记(十)Flink容错机制
1922. Count Good Numbers
一款开源的Markdown转富文本编辑器的实现原理剖析
Spark 结构化流写入Hudi 实践
Flink-CDC实践(含实操步骤与截图)
Vscode编辑器右键没有Open In Default Browser选项
PIP configuring domestic sources
Logstash+jdbc data synchronization +head display problems
LeetCode每日一题(2109. Adding Spaces to a String)
The rise and fall of mobile phones in my perspective these 10 years