Problem 1024: Video Stitching

This commit is contained in:
DarkCat09 2023-07-07 21:25:31 +04:00
commit cd8655989a
4 changed files with 116 additions and 0 deletions

1
video-stitching/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
video-stitching/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "video-stitching"
version = "0.1.0"

View file

@ -0,0 +1,8 @@
[package]
name = "video-stitching"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

100
video-stitching/src/main.rs Normal file
View file

@ -0,0 +1,100 @@
fn video_stitching(clips: Vec<Vec<i32>>, time: i32) -> i32 {
let mut arr = clips.clone();
arr.sort_by(|a, b|
if a[0] == b[0] {
b[1].cmp(&a[1])
}
else {
a[0].cmp(&b[0])
}
);
if arr[0][0] != 0 {
return -1;
}
let mut current_index = 0;
let mut current_time = arr[0][1];
let mut count = 1;
while current_time < time {
let mut max: Option<&Vec<i32>> = None;
let mut max_index: Option<usize> = None;
for (index, slice) in arr.iter().enumerate() {
if index <= current_index {
continue;
}
if slice[0] > current_time {
break;
}
if max.is_none() || slice[1] > max.unwrap()[1] {
max = Some(slice);
max_index = Some(index);
}
}
if let (Some(slice), Some(index)) = (max, max_index) {
current_time = slice[1];
current_index = index;
count += 1;
}
else {
return -1;
}
}
count
}
fn main() {
println!("{}", video_stitching(vec![vec![0,1]], 1));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple() {
let clips = vec![vec![0,1], vec![1,3], vec![3,7]];
assert_eq!(video_stitching(clips, 7), 3);
}
#[test]
fn test_ex1() {
let clips = vec![
vec![0,2], vec![4,6], vec![8,10],
vec![1,9], vec![1,5], vec![5,9],
];
assert_eq!(video_stitching(clips, 10), 3);
}
#[test]
fn test_ex2() {
let clips = vec![vec![0,1], vec![1,2]];
assert_eq!(video_stitching(clips, 5), -1);
}
#[test]
fn test_ex3() {
let clips = vec![
vec![0,1], vec![6,8], vec![0,2], vec![5,6],
vec![0,4], vec![0,3], vec![6,7], vec![1,3],
vec![4,7], vec![1,4], vec![2,5], vec![2,6],
vec![3,4], vec![4,5], vec![5,7], vec![6,9],
];
assert_eq!(video_stitching(clips, 9), 3);
}
#[test]
fn test_n48() {
let clips = vec![
vec![ 8,10], vec![17,39], vec![18,19], vec![ 8,16],
vec![13,35], vec![33,39], vec![11,19], vec![18,35],
];
assert_eq!(video_stitching(clips, 20), -1);
}
}