commit 8468f55e0eb7293014e8ce34a77af70dda6f710f Author: li-chx Date: Thu Jul 10 14:13:01 2025 +0800 3440. 重新安排会议得到最多空余时间 II diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b60de5b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..5800ba0 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,73 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5db94b4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/rust_test.iml b/.idea/rust_test.iml new file mode 100644 index 0000000..45b3867 --- /dev/null +++ b/.idea/rust_test.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..370f21e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "rust_test" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ea249d6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_test" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..40a6225 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,82 @@ +use std::cmp::max; + +struct Solution; +impl Solution { + pub fn max_free_time(event_time: i32, start_time: Vec, end_time: Vec) -> i32 { + let mut first_selection = 0; + let mut second_selection = 0; + let mut third_selection = 0; + + if end_time[end_time.len() - 1] < event_time { + first_selection = event_time - end_time[end_time.len() - 1]; + } + let mut last_time = 0; + for i in 0..start_time.len() { + if start_time[i] > last_time { + let val = start_time[i] - last_time; + if val > first_selection { + third_selection = second_selection; + second_selection = first_selection; + first_selection = val; + } else if val > second_selection { + third_selection = second_selection; + second_selection = val; + } else if val > third_selection { + third_selection = val; + } + } + last_time = end_time[i]; + } + + let check_can_add = |left_space: i32, right_space: i32, needed: i32| -> bool { + if needed <= third_selection { + return true; + } + if needed <= second_selection { + if left_space == second_selection && right_space == first_selection || + left_space == first_selection && right_space == second_selection { + return false; + } + return true; + } + if needed <= first_selection { + if left_space != first_selection && right_space != first_selection { + return true; + } + } + return false; + }; + last_time = end_time[0]; + let mut last = start_time[0]; + let mut ans = start_time[0]; + for i in 1..start_time.len() { + let left_space = last; + let right_space = start_time[i] - last_time; + let new_val = if check_can_add(left_space, right_space, end_time[i - 1] - start_time[i - 1]) { + left_space + right_space + end_time[i - 1] - start_time[i - 1] + } else { + left_space + right_space + }; + ans = max(ans, new_val); + last = right_space; + last_time = end_time[i]; + } + { + let left_space = last; + let right_space = event_time - last_time; + let new_val = if check_can_add(left_space, right_space, end_time[end_time.len() - 1] - start_time[end_time.len() - 1]) { + left_space + right_space + end_time[end_time.len() - 1] + - start_time[end_time.len() - 1] + } else { + left_space + right_space + }; + ans = max(ans, new_val); + } + ans + } +} + +fn main() { + let sl = Solution::max_free_time(5, vec![1, 3], vec![2, 5]); + println!("{}", sl); +} diff --git a/src/test_mod/Cargo.lock b/src/test_mod/Cargo.lock new file mode 100644 index 0000000..0ae2991 --- /dev/null +++ b/src/test_mod/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "test_mod" +version = "0.1.0" diff --git a/src/test_mod/Cargo.toml b/src/test_mod/Cargo.toml new file mode 100644 index 0000000..c32dd78 --- /dev/null +++ b/src/test_mod/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "test_mod" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/test_mod/src/main.rs b/src/test_mod/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/test_mod/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/testmod.rs b/src/testmod.rs new file mode 100644 index 0000000..7c7e72f --- /dev/null +++ b/src/testmod.rs @@ -0,0 +1 @@ +mod test_mod; \ No newline at end of file