Skip to content

Commit 64366a2

Browse files
committed
Add problem 2974: Minimum Number Game
1 parent 8cabc05 commit 64366a2

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,7 @@ pub mod problem_2965_find_missing_and_repeated_values;
20352035
pub mod problem_2966_divide_array_into_arrays_with_max_difference;
20362036
pub mod problem_2970_count_the_number_of_incremovable_subarrays_i;
20372037
pub mod problem_2972_count_the_number_of_incremovable_subarrays_ii;
2038+
pub mod problem_2974_minimum_number_game;
20382039

20392040
#[cfg(test)]
20402041
mod test_utilities;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod sorting;
2+
3+
pub trait Solution {
4+
fn number_game(nums: Vec<i32>) -> Vec<i32>;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&[5, 4, 2, 3] as &[_], &[3, 2, 5, 4] as &[_]), (&[2, 5], &[5, 2])];
13+
14+
for (nums, expected) in test_cases {
15+
assert_eq!(S::number_game(nums.to_vec()), expected);
16+
}
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
7+
let mut nums = nums;
8+
9+
nums.sort_unstable_by_key(|num| num.cast_unsigned());
10+
11+
for chunk in nums.chunks_exact_mut(2) {
12+
chunk.swap(0, 1);
13+
}
14+
15+
nums
16+
}
17+
}
18+
19+
// ------------------------------------------------------ snip ------------------------------------------------------ //
20+
21+
impl super::Solution for Solution {
22+
fn number_game(nums: Vec<i32>) -> Vec<i32> {
23+
Self::number_game(nums)
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
#[test]
30+
fn test_solution() {
31+
super::super::tests::run::<super::Solution>();
32+
}
33+
}

0 commit comments

Comments
 (0)