From cd8655989aa70ef7a1bee85fd4b8dd2c4ca7ec14 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Fri, 7 Jul 2023 21:25:31 +0400 Subject: [PATCH] Problem 1024: Video Stitching --- video-stitching/.gitignore | 1 + video-stitching/Cargo.lock | 7 +++ video-stitching/Cargo.toml | 8 +++ video-stitching/src/main.rs | 100 ++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 video-stitching/.gitignore create mode 100644 video-stitching/Cargo.lock create mode 100644 video-stitching/Cargo.toml create mode 100644 video-stitching/src/main.rs diff --git a/video-stitching/.gitignore b/video-stitching/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/video-stitching/.gitignore @@ -0,0 +1 @@ +/target diff --git a/video-stitching/Cargo.lock b/video-stitching/Cargo.lock new file mode 100644 index 0000000..b0b5131 --- /dev/null +++ b/video-stitching/Cargo.lock @@ -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" diff --git a/video-stitching/Cargo.toml b/video-stitching/Cargo.toml new file mode 100644 index 0000000..c53c1a9 --- /dev/null +++ b/video-stitching/Cargo.toml @@ -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] diff --git a/video-stitching/src/main.rs b/video-stitching/src/main.rs new file mode 100644 index 0000000..4f431b5 --- /dev/null +++ b/video-stitching/src/main.rs @@ -0,0 +1,100 @@ +fn video_stitching(clips: Vec>, 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> = None; + let mut max_index: Option = 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); + } +}