-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1887: Reduction Operations to Make the Array Elements Equal
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/problem_1887_reduction_operations_to_make_the_array_elements_equal/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Reverse; | ||
|
||
impl Solution { | ||
pub fn reduction_operations(nums: Vec<i32>) -> i32 { | ||
let mut nums = nums; | ||
|
||
nums.sort_unstable_by_key(|&num| Reverse(num as u32)); | ||
|
||
let mut result = 0; | ||
let mut prev = 0; | ||
|
||
for (count, num) in (0..).zip(nums) { | ||
if num != prev { | ||
result += count; | ||
prev = num; | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn reduction_operations(nums: Vec<i32>) -> i32 { | ||
Self::reduction_operations(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_1887_reduction_operations_to_make_the_array_elements_equal/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn reduction_operations(nums: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(&[5, 1, 3] as &[_], 3), (&[1, 1, 1], 0), (&[1, 1, 2, 2, 3], 4)]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::reduction_operations(nums.to_vec()), expected); | ||
} | ||
} | ||
} |