-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1986: Minimum Number of Work Sessions to Finish the Tasks
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/problem_1986_minimum_number_of_work_sessions_to_finish_the_tasks/dynamic_programming.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn min_sessions(tasks: Vec<i32>, session_time: i32) -> i32 { | ||
let session_time = session_time as u8; | ||
let total_combinations = 1 << tasks.len(); | ||
let mut cache = vec![(0_u8, 0_u8); total_combinations].into_boxed_slice(); | ||
|
||
cache[0].1 = session_time; | ||
|
||
for combination in 1..total_combinations { | ||
let mut bits = combination; | ||
let mut best = (u8::MAX, u8::MAX); | ||
|
||
loop { | ||
let last_task = bits.trailing_zeros(); | ||
let last_bit = 1 << last_task; | ||
let prev_session = cache[combination ^ last_bit]; | ||
let task_time = tasks[last_task as usize] as u8; | ||
let candidate = prev_session.1 + task_time; | ||
|
||
best = best.min(if candidate <= session_time { | ||
(prev_session.0, candidate) | ||
} else { | ||
(prev_session.0 + 1, task_time) | ||
}); | ||
|
||
bits ^= last_bit; | ||
|
||
if bits == 0 { | ||
break; | ||
} | ||
} | ||
|
||
cache[combination] = best; | ||
} | ||
|
||
i32::from(cache.last().unwrap().0) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn min_sessions(tasks: Vec<i32>, session_time: i32) -> i32 { | ||
Self::min_sessions(tasks, session_time) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_1986_minimum_number_of_work_sessions_to_finish_the_tasks/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pub mod dynamic_programming; | ||
|
||
pub trait Solution { | ||
fn min_sessions(tasks: Vec<i32>, session_time: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[1, 2, 3] as &[_], 3), 2), | ||
((&[3, 1, 3, 1, 1], 8), 2), | ||
((&[1, 2, 3, 4, 5], 15), 1), | ||
]; | ||
|
||
for ((tasks, session_time), expected) in test_cases { | ||
assert_eq!(S::min_sessions(tasks.to_vec(), session_time), expected); | ||
} | ||
} | ||
} |