From ecb86cb5e5d395708a6aba4f78dd295d83cd4169 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sun, 14 Jan 2024 22:05:30 +0800 Subject: [PATCH] Add problem 1751: Maximum Number of Events That Can Be Attended II --- src/lib.rs | 1 + .../dynamic_programming.rs | 74 +++++++++++++++++++ .../mod.rs | 23 ++++++ 3 files changed, 98 insertions(+) create mode 100644 src/problem_1751_maximum_number_of_events_that_can_be_attended_ii/dynamic_programming.rs create mode 100644 src/problem_1751_maximum_number_of_events_that_can_be_attended_ii/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 7ae14ea9..5cc96ca5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1306,6 +1306,7 @@ pub mod problem_1745_palindrome_partitioning_iv; pub mod problem_1748_sum_of_unique_elements; pub mod problem_1749_maximum_absolute_sum_of_any_subarray; pub mod problem_1750_minimum_length_of_string_after_deleting_similar_ends; +pub mod problem_1751_maximum_number_of_events_that_can_be_attended_ii; pub mod problem_1752_check_if_array_is_sorted_and_rotated; pub mod problem_1753_maximum_score_from_removing_stones; pub mod problem_1754_largest_merge_of_two_strings; diff --git a/src/problem_1751_maximum_number_of_events_that_can_be_attended_ii/dynamic_programming.rs b/src/problem_1751_maximum_number_of_events_that_can_be_attended_ii/dynamic_programming.rs new file mode 100644 index 00000000..200f93db --- /dev/null +++ b/src/problem_1751_maximum_number_of_events_that_can_be_attended_ii/dynamic_programming.rs @@ -0,0 +1,74 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::cell::Cell; +use std::convert::TryInto; + +impl Solution { + pub fn max_value(events: Vec>, k: i32) -> i32 { + let mut events = events + .into_iter() + .map(|event| { + let [start, end, value]: [_; 3] = event.try_into().ok().unwrap(); + + (end as u32, start as u32, value as u32, Cell::new(0_u32)) + }) + .collect::>(); + + let k = k as u32 as usize; + + events.sort_unstable(); + + for (i, event) in events.iter().enumerate() { + event.3.set( + events[..i] + .partition_point(|prev_event| prev_event.0 < event.1) + .wrapping_sub(1) as _, + ); + } + + let mut cache = vec![0; k * events.len()].into_boxed_slice(); + + for (i, event) in events.iter().enumerate() { + let (prev_rows, row) = cache[..k * (i + 1)].split_at_mut(k * i); + + for (j, value) in row.iter_mut().enumerate() { + let value_if_drop = prev_rows + .get(k.wrapping_mul(i.wrapping_sub(1)).wrapping_add(j)) + .copied() + .unwrap_or(0); + + let value_if_choose = if j == 0 { + event.2 + } else { + prev_rows + .get(k.wrapping_mul(event.3.get() as usize).wrapping_add(j - 1)) + .copied() + .unwrap_or(0) + + event.2 + }; + + *value = value_if_drop.max(value_if_choose); + } + } + + *cache.last().unwrap() as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn max_value(events: Vec>, k: i32) -> i32 { + Self::max_value(events, k) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1751_maximum_number_of_events_that_can_be_attended_ii/mod.rs b/src/problem_1751_maximum_number_of_events_that_can_be_attended_ii/mod.rs new file mode 100644 index 00000000..db0cdfe6 --- /dev/null +++ b/src/problem_1751_maximum_number_of_events_that_can_be_attended_ii/mod.rs @@ -0,0 +1,23 @@ +pub mod dynamic_programming; + +pub trait Solution { + fn max_value(events: Vec>, k: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ((&[[1, 2, 4], [3, 4, 3], [2, 3, 1]] as &[_], 2), 7), + ((&[[1, 2, 4], [3, 4, 3], [2, 3, 10]], 2), 10), + ((&[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]], 3), 9), + ((&[[30, 40, 34], [6, 11, 6], [60, 81, 36]], 1), 36), + ]; + + for ((events, k), expected) in test_cases { + assert_eq!(S::max_value(events.iter().map(Vec::from).collect(), k), expected,); + } + } +}