-
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 1626: Best Team With No Conflicts
- Loading branch information
Showing
3 changed files
with
87 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
56 changes: 56 additions & 0 deletions
56
src/problem_1626_best_team_with_no_conflicts/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,56 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn best_team_score(scores: Vec<i32>, ages: Vec<i32>) -> i32 { | ||
let mut players = scores | ||
.iter() | ||
.zip(&ages) | ||
.map(|(&score, &age)| (age as u32, score as u32)) | ||
.collect::<Box<_>>(); | ||
|
||
players.sort_unstable(); | ||
|
||
drop(ages); | ||
|
||
let mut cache = scores; // Reuse already allocated memory. | ||
let mut result = 0; | ||
|
||
#[allow(clippy::unnecessary_lazy_evaluations)] // Not supported. | ||
for (i, &(age, score)) in players.iter().enumerate() { | ||
let (target, left_cache) = cache[..=i].split_last_mut().unwrap(); | ||
|
||
let total_score = left_cache | ||
.iter() | ||
.zip(&*players) | ||
.filter_map(|(&left_total_score, &(left_age, left_score))| { | ||
(left_age == age || left_score <= score).then(|| left_total_score as u32) | ||
}) | ||
.max() | ||
.map_or(score, |left_score| left_score + score); | ||
|
||
*target = total_score as _; | ||
|
||
result = result.max(total_score); | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn best_team_score(scores: Vec<i32>, ages: Vec<i32>) -> i32 { | ||
Self::best_team_score(scores, ages) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,30 @@ | ||
pub mod dynamic_programming; | ||
|
||
pub trait Solution { | ||
fn best_team_score(scores: Vec<i32>, ages: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[1, 3, 5, 10, 15] as &[_], &[1, 2, 3, 4, 5] as &[_]), 34), | ||
((&[4, 5, 6, 5], &[2, 1, 2, 1]), 16), | ||
((&[1, 2, 3, 5], &[8, 9, 10, 1]), 6), | ||
( | ||
( | ||
&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], | ||
&[811, 364, 124, 873, 790, 656, 581, 446, 885, 134], | ||
), | ||
10, | ||
), | ||
((&[1, 3, 7, 3, 2, 4, 10, 7, 5], &[4, 5, 2, 1, 1, 2, 4, 1, 4]), 29), | ||
]; | ||
|
||
for ((scores, ages), expected) in test_cases { | ||
assert_eq!(S::best_team_score(scores.to_vec(), ages.to_vec()), expected); | ||
} | ||
} | ||
} |