From 9918f7f6ad320200550782240c65574b12ce135f Mon Sep 17 00:00:00 2001 From: Spxg Date: Fri, 20 Dec 2024 13:43:18 +0800 Subject: [PATCH] Add problem 1834: Single-Threaded CPU --- src/lib.rs | 1 + .../binary_heap.rs | 67 +++++++++++++++++++ src/problem_1834_single_threaded_cpu/mod.rs | 49 ++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 src/problem_1834_single_threaded_cpu/binary_heap.rs create mode 100644 src/problem_1834_single_threaded_cpu/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 3a2866f..d00d687 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -591,6 +591,7 @@ pub mod problem_1780_check_if_number_is_a_sum_of_powers_of_three; pub mod problem_1797_design_authentication_manager; pub mod problem_1798_maximum_number_of_consecutive_values_you_can_make; pub mod problem_1807_evaluate_the_bracket_pairs_of_a_string; +pub mod problem_1834_single_threaded_cpu; pub mod problem_2348_number_of_zero_filled_subarrays; #[cfg(test)] diff --git a/src/problem_1834_single_threaded_cpu/binary_heap.rs b/src/problem_1834_single_threaded_cpu/binary_heap.rs new file mode 100644 index 0000000..322df83 --- /dev/null +++ b/src/problem_1834_single_threaded_cpu/binary_heap.rs @@ -0,0 +1,67 @@ +pub struct Solution; + +use std::collections::BinaryHeap; + +impl Solution { + pub fn get_order(tasks: Vec>) -> Vec { + #[derive(Eq, PartialEq)] + struct Helper(i32, i32, usize); + + impl Ord for Helper { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + (self.1, self.2).cmp(&(other.1, other.2)).reverse() + } + } + + impl PartialOrd for Helper { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } + } + + let mut result = Vec::with_capacity(tasks.len()); + let mut heap = BinaryHeap::::with_capacity(tasks.len()); + let mut tasks = tasks + .into_iter() + .enumerate() + .map(|(idx, nums)| (nums[0], nums[1], idx)) + .collect::>(); + tasks.sort_unstable(); + + let mut current = 0; + + 'task_for: for task in tasks { + while task.0 > current { + if let Some(helper) = heap.pop() { + result.push(helper.2 as _); + current += helper.1; + } else { + result.push(task.2 as _); + current = task.0 + task.1; + continue 'task_for; + } + } + heap.push(Helper(task.0, task.1, task.2)); + } + + while let Some(helper) = heap.pop() { + result.push(helper.2 as _); + } + + result + } +} + +impl super::Solution for Solution { + fn get_order(tasks: Vec>) -> Vec { + Self::get_order(tasks) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1834_single_threaded_cpu/mod.rs b/src/problem_1834_single_threaded_cpu/mod.rs new file mode 100644 index 0000000..791bf15 --- /dev/null +++ b/src/problem_1834_single_threaded_cpu/mod.rs @@ -0,0 +1,49 @@ +pub mod binary_heap; + +pub trait Solution { + fn get_order(tasks: Vec>) -> Vec; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ( + &[[1, 2], [2, 4], [3, 2], [4, 1]] as &[_], + &[0, 2, 3, 1] as &[_], + ), + ( + &[[7, 10], [7, 12], [7, 5], [7, 4], [7, 2]], + &[4, 3, 2, 0, 1], + ), + ( + &[ + [19, 13], + [16, 9], + [21, 10], + [32, 25], + [37, 4], + [49, 24], + [2, 15], + [38, 41], + [37, 34], + [33, 6], + [45, 4], + [18, 18], + [46, 39], + [12, 24], + ], + &[6, 1, 2, 9, 4, 10, 0, 11, 5, 13, 3, 8, 12, 7], + ), + ]; + + for (tasks, expected) in test_cases { + assert_eq!( + S::get_order(tasks.iter().map(Vec::from).collect()), + expected + ); + } + } +}