Skip to content

Commit c01bd6c

Browse files
committed
Add problem 2895: Minimum Processing Time
1 parent ad2d9e8 commit c01bd6c

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,7 @@ pub mod problem_2871_split_array_into_maximum_number_of_subarrays;
20022002
pub mod problem_2873_maximum_value_of_an_ordered_triplet_i;
20032003
pub mod problem_2874_maximum_value_of_an_ordered_triplet_ii;
20042004
pub mod problem_2894_divisible_and_non_divisible_sums_difference;
2005+
pub mod problem_2895_minimum_processing_time;
20052006

20062007
#[cfg(test)]
20072008
mod test_utilities;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn min_processing_time(processor_time: Vec<i32>, tasks: Vec<i32>) -> i32 {
7+
let mut processor_time = processor_time;
8+
let mut tasks = tasks;
9+
10+
processor_time.sort_unstable();
11+
tasks.sort_unstable();
12+
13+
processor_time
14+
.iter()
15+
.zip(tasks.iter().rev().step_by(4))
16+
.fold(0, |max, (processor_time, task)| {
17+
u32::max(max, (processor_time + task) as _)
18+
}) as _
19+
}
20+
}
21+
22+
// ------------------------------------------------------ snip ------------------------------------------------------ //
23+
24+
impl super::Solution for Solution {
25+
fn min_processing_time(processor_time: Vec<i32>, tasks: Vec<i32>) -> i32 {
26+
Self::min_processing_time(processor_time, tasks)
27+
}
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
#[test]
33+
fn test_solution() {
34+
super::super::tests::run::<super::Solution>();
35+
}
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn min_processing_time(processor_time: Vec<i32>, tasks: Vec<i32>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
((&[8, 10] as &[_], &[2, 2, 3, 1, 8, 7, 4, 5] as &[_]), 16),
14+
((&[10, 20], &[2, 3, 1, 2, 5, 8, 4, 3]), 23),
15+
];
16+
17+
for ((processor_time, tasks), expected) in test_cases {
18+
assert_eq!(
19+
S::min_processing_time(processor_time.to_vec(), tasks.to_vec()),
20+
expected,
21+
);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)