Skip to content

Commit

Permalink
Add problem 2242: Maximum Score of a Node Sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Dec 7, 2024
1 parent 92801d2 commit 08dbebe
Show file tree
Hide file tree
Showing 3 changed files with 149 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 @@ -1671,6 +1671,7 @@ pub mod problem_2235_add_two_integers;
pub mod problem_2236_root_equals_sum_of_children;
pub mod problem_2239_find_closest_number_to_zero;
pub mod problem_2241_design_an_atm_machine;
pub mod problem_2242_maximum_score_of_a_node_sequence;
pub mod problem_2243_calculate_digit_sum_of_a_string;
pub mod problem_2244_minimum_rounds_to_complete_all_tasks;
pub mod problem_2245_maximum_trailing_zeros_in_a_cornered_path;
Expand Down
95 changes: 95 additions & 0 deletions src/problem_2242_maximum_score_of_a_node_sequence/greedy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
fn insert_neighbor((scores, neighbors): &mut ([u32; 3], [u16; 3]), score: u32, neighbor: u16) {
if score > scores[2] {
let mut i = 2;

while i != 0 {
let next_i = i - 1;

if score > scores[next_i] {
scores[i] = scores[next_i];
neighbors[i] = neighbors[next_i];
i = next_i;
} else {
break;
}
}

scores[i] = score;
neighbors[i] = neighbor;
}
}

pub fn maximum_score(scores: Vec<i32>, edges: Vec<Vec<i32>>) -> i32 {
let n = scores.len();
let mut nodes = vec![([0_u32; 3], [0_u16; 3]); n].into_boxed_slice();

let edges = edges
.into_iter()
.map(|edge| {
let [from, to] = <[_; 2]>::map(edge.as_slice().try_into().ok().unwrap(), |x| x as u32 as usize);

Self::insert_neighbor(&mut nodes[from], scores[to] as _, to as _);
Self::insert_neighbor(&mut nodes[to], scores[from] as _, from as _);

(from as u16, to as u16)
})
.collect::<Box<_>>();

let mut result = -1;

for &(from, to) in &*edges {
let (from_usize, to_usize) = (usize::from(from), usize::from(to));
let middle_score = scores[from_usize] + scores[to_usize];
let (from_node, to_node) = (&nodes[from_usize], &nodes[to_usize]);

for i in 0..3 {
let from_neighbor_score = from_node.0[i] as i32;

if from_neighbor_score == 0 {
break;
}

let from_neighbor = from_node.1[i];

if from_neighbor != from && from_neighbor != to {
for j in 0..3 {
let to_neighbor_score = to_node.0[j] as i32;

if to_neighbor_score == 0 {
break;
}

let to_neighbor = to_node.1[j];

if to_neighbor != to && to_neighbor != from && to_neighbor != from_neighbor {
result = result.max(middle_score + from_neighbor_score + to_neighbor_score);
}
}
}
}
}

result
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn maximum_score(scores: Vec<i32>, edges: Vec<Vec<i32>>) -> i32 {
Self::maximum_score(scores, edges)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
53 changes: 53 additions & 0 deletions src/problem_2242_maximum_score_of_a_node_sequence/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
pub mod greedy;

pub trait Solution {
fn maximum_score(scores: Vec<i32>, edges: Vec<Vec<i32>>) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
(
(
&[5, 2, 9, 8, 4] as &[_],
&[[0, 1], [1, 2], [2, 3], [0, 2], [1, 3], [2, 4]] as &[_],
),
24,
),
((&[9, 20, 6, 4, 11, 12], &[[0, 3], [5, 3], [2, 4], [1, 3]]), -1),
(
(
&[18, 6, 4, 9, 8, 2],
&[
[0, 1],
[0, 2],
[0, 3],
[0, 4],
[0, 5],
[1, 2],
[1, 3],
[1, 4],
[1, 5],
[2, 3],
[2, 4],
[2, 5],
[3, 4],
[3, 5],
[4, 5],
],
),
41,
),
];

for ((scores, edges), expected) in test_cases {
assert_eq!(
S::maximum_score(scores.to_vec(), edges.iter().map(Vec::from).collect()),
expected,
);
}
}
}

0 comments on commit 08dbebe

Please sign in to comment.