Skip to content

Commit

Permalink
Add problem 2293: Min Max Game
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 24, 2024
1 parent 6762fef commit d57ab30
Show file tree
Hide file tree
Showing 3 changed files with 67 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 @@ -1701,6 +1701,7 @@ pub mod problem_2284_sender_with_largest_word_count;
pub mod problem_2285_maximum_total_importance_of_roads;
pub mod problem_2287_rearrange_characters_to_make_target_string;
pub mod problem_2288_apply_discount_to_prices;
pub mod problem_2293_min_max_game;

#[cfg(test)]
mod test_utilities;
48 changes: 48 additions & 0 deletions src/problem_2293_min_max_game/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pub struct Solution;

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

impl Solution {
pub fn min_max_game(nums: Vec<i32>) -> i32 {
let mut nums = nums;
let n = nums.len().trailing_zeros();

for i in 0..n {
let small_skip = 1 << i;
let big_skip = small_skip << 2;
let mut j = small_skip;

while j < nums.len() {
nums[j - small_skip] = nums[j - small_skip].min(nums[j]);

j += big_skip;
}

j = small_skip * 3;

while j < nums.len() {
nums[j - small_skip] = nums[j - small_skip].max(nums[j]);

j += big_skip;
}
}

nums[0]
}
}

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

impl super::Solution for Solution {
fn min_max_game(nums: Vec<i32>) -> i32 {
Self::min_max_game(nums)
}
}

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

pub trait Solution {
fn min_max_game(nums: Vec<i32>) -> i32;
}

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

pub fn run<S: Solution>() {
let test_cases = [(&[1, 3, 5, 2, 4, 8, 2, 2] as &[_], 1), (&[3], 3)];

for (nums, expected) in test_cases {
assert_eq!(S::min_max_game(nums.to_vec()), expected);
}
}
}

0 comments on commit d57ab30

Please sign in to comment.