Skip to content

Commit

Permalink
Add problem 1626: Best Team With No Conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 2, 2023
1 parent d3935d9 commit 30d3f5a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,7 @@ pub mod problem_1621_number_of_sets_of_k_non_overlapping_line_segments;
pub mod problem_1622_fancy_sequence;
pub mod problem_1624_largest_substring_between_two_equal_characters;
pub mod problem_1625_lexicographically_smallest_string_after_applying_operations;
pub mod problem_1626_best_team_with_no_conflicts;
pub mod problem_1629_slowest_key;
pub mod problem_1630_arithmetic_subarrays;
pub mod problem_1631_path_with_minimum_effort;
Expand Down
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>();
}
}
30 changes: 30 additions & 0 deletions src/problem_1626_best_team_with_no_conflicts/mod.rs
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);
}
}
}

0 comments on commit 30d3f5a

Please sign in to comment.