-
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 2366: Minimum Replacements to Sort the Array
- Loading branch information
Showing
3 changed files
with
69 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
46 changes: 46 additions & 0 deletions
46
src/problem_2366_minimum_replacements_to_sort_the_array/greedy.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,46 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn minimum_replacement(nums: Vec<i32>) -> i64 { | ||
let mut result = 0; | ||
let mut right = u32::MAX; | ||
|
||
for &num in nums.iter().rev() { | ||
let num = num as u32; | ||
|
||
if right < num { | ||
let quotient = num / right; | ||
let remainder = num % right; | ||
|
||
if remainder == 0 { | ||
result += u64::from(quotient - 1); | ||
} else { | ||
result += u64::from(quotient); | ||
right = num / (quotient + 1); | ||
} | ||
} else { | ||
right = num; | ||
} | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_replacement(nums: Vec<i32>) -> i64 { | ||
Self::minimum_replacement(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_2366_minimum_replacements_to_sort_the_array/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,22 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn minimum_replacement(nums: Vec<i32>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[3, 9, 3] as &[_], 2), | ||
(&[1, 2, 3, 4, 5], 0), | ||
(&[12, 9, 7, 6, 17, 19, 21], 6), | ||
]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::minimum_replacement(nums.to_vec()), expected); | ||
} | ||
} | ||
} |