Skip to content

Commit

Permalink
feat: add maximum.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
changweidalaifu6 committed May 24, 2024
1 parent c2009cd commit 10394fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [Sudoku](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/sudoku.rs)
* Big Integer
* [Fast Factorial](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/fast_factorial.rs)
* [Maximum](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/maximum.rs)
* [Multiply](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/multiply.rs)
* [Poly1305](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/poly1305.rs)
* Bit Manipulation
Expand Down
33 changes: 33 additions & 0 deletions src/big_integer/maximum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// Compute the multiplication set of the three largest numbers in an array
pub fn maximum(mut nums: Vec<i32>) -> i32 {
nums.sort();
(nums[nums.len() - 1] * nums[nums.len() - 2] * nums[nums.len() - 3])
.max(nums[0] * nums[nums.len() - 1] * nums[nums.len() - 2])
.max(nums[0] * nums[1] * nums[nums.len() - 1])
}

#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_maximum {
($($name:ident: $inputs:expr,)*) => {
$(
#[test]
fn $name() {
let (s, expected) = $inputs;
assert_eq!(maximum(s.to_vec()), expected);
}
)*
}
}

test_maximum! {
positive0: ([1,2,3], 6),
positive1: ([1,2,3,4], 24),
negative0: ([-1,-2,-3], -6),
negative1: ([-1,-2,-3,-4,-5], -6),
positive_and_negative0: ([-1,-2,-3,4,5], 30),
positive_and_negative1: ([-1,2,-3,-4,5], 60),
positive_and_negative2: ([-1,2,-3,4,5], 40),
}
}
2 changes: 2 additions & 0 deletions src/big_integer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![cfg(feature = "big-math")]

mod fast_factorial;
mod maximum;
mod multiply;
mod poly1305;

pub use self::fast_factorial::fast_factorial;
pub use self::maximum::maximum;
pub use self::multiply::multiply;
pub use self::poly1305::Poly1305;

0 comments on commit 10394fa

Please sign in to comment.