Skip to content

Commit 86de325

Browse files
committed
Add problem 3038: Maximum Number of Operations With the Same Score I
1 parent 554de6b commit 86de325

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,7 @@ pub mod problem_3026_maximum_good_subarray_sum;
20562056
pub mod problem_3028_ant_on_the_boundary;
20572057
pub mod problem_3033_modify_the_matrix;
20582058
pub mod problem_3035_maximum_palindromes_after_operations;
2059+
pub mod problem_3038_maximum_number_of_operations_with_the_same_score_i;
20592060

20602061
#[cfg(test)]
20612062
mod test_utilities;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn max_operations(nums: Vec<i32>) -> i32 {
7+
let mut iter = nums.chunks_exact(2).map(|chunk| chunk[0] + chunk[1]);
8+
let first = iter.next().unwrap();
9+
10+
(iter.take_while(|&sum| sum == first).count() + 1) as _
11+
}
12+
}
13+
14+
// ------------------------------------------------------ snip ------------------------------------------------------ //
15+
16+
impl super::Solution for Solution {
17+
fn max_operations(nums: Vec<i32>) -> i32 {
18+
Self::max_operations(nums)
19+
}
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
#[test]
25+
fn test_solution() {
26+
super::super::tests::run::<super::Solution>();
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn max_operations(nums: Vec<i32>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
(&[3, 2, 1, 4, 5] as &[_], 2),
14+
(&[1, 5, 3, 3, 4, 1, 3, 2, 2, 3], 2),
15+
(&[5, 3], 1),
16+
];
17+
18+
for (nums, expected) in test_cases {
19+
assert_eq!(S::max_operations(nums.to_vec()), expected);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)