Skip to content

Commit beb6b08

Browse files
committed
Add problem 3282: Reach End of Array With Max Score
1 parent a6b63b7 commit beb6b08

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,7 @@ pub mod problem_3274_check_if_two_chessboard_squares_have_the_same_color;
21682168
pub mod problem_3275_k_th_nearest_obstacle_queries;
21692169
pub mod problem_3280_convert_date_to_binary;
21702170
pub mod problem_3281_maximize_score_of_numbers_in_ranges;
2171+
pub mod problem_3282_reach_end_of_array_with_max_score;
21712172
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21722173

21732174
#[cfg(test)]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn find_maximum_score(nums: Vec<i32>) -> i64 {
7+
let mut nums = nums;
8+
let mut prev = 0;
9+
10+
nums.pop();
11+
12+
nums.into_iter()
13+
.map(|num| {
14+
prev = prev.max(u64::from(num.cast_unsigned()));
15+
16+
prev
17+
})
18+
.sum::<u64>()
19+
.cast_signed()
20+
}
21+
}
22+
23+
// ------------------------------------------------------ snip ------------------------------------------------------ //
24+
25+
impl super::Solution for Solution {
26+
fn find_maximum_score(nums: Vec<i32>) -> i64 {
27+
Self::find_maximum_score(nums)
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
#[test]
34+
fn test_solution() {
35+
super::super::tests::run::<super::Solution>();
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn find_maximum_score(nums: Vec<i32>) -> i64;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&[1, 3, 1, 5] as &[_], 7), (&[4, 3, 1, 3, 2], 16)];
13+
14+
for (nums, expected) in test_cases {
15+
assert_eq!(S::find_maximum_score(nums.to_vec()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)