Skip to content

Commit

Permalink
Add problem 2358: Maximum Number of Groups Entering a Competition
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Dec 11, 2024
1 parent 5389380 commit e0fbca7
Show file tree
Hide file tree
Showing 3 changed files with 56 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 @@ -1749,6 +1749,7 @@ pub mod problem_2351_first_letter_to_appear_twice;
pub mod problem_2352_equal_row_and_column_pairs;
pub mod problem_2353_design_a_food_rating_system;
pub mod problem_2357_make_array_zero_by_subtracting_equal_amounts;
pub mod problem_2358_maximum_number_of_groups_entering_a_competition;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod newtons_method;

pub trait Solution {
fn maximum_groups(grades: Vec<i32>) -> i32;
}

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

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

for (grades, expected) in test_cases {
assert_eq!(S::maximum_groups(grades.to_vec()), expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub struct Solution;

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

impl Solution {
pub fn maximum_groups(grades: Vec<i32>) -> i32 {
let n = grades.len();
let n_times_2 = n * 2;
let mut guess = n;

loop {
let new_guess = (guess * guess + n_times_2) / (guess * 2 + 1);

if new_guess < guess {
guess = new_guess;
} else {
return guess as _;
}
}
}
}

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

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

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}

0 comments on commit e0fbca7

Please sign in to comment.