-
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 2064: Minimized Maximum of Products Distributed to Any Store
- Loading branch information
Showing
3 changed files
with
79 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
56 changes: 56 additions & 0 deletions
56
src/problem_2064_minimized_maximum_of_products_distributed_to_any_store/binary_search.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,56 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::num::NonZeroU32; | ||
|
||
impl Solution { | ||
fn check(mut n: u32, quantities: &[i32], limit: NonZeroU32) -> bool { | ||
for &quantity in quantities { | ||
let required = (quantity as u32 + limit.get() - 1) / limit; | ||
|
||
if n < required { | ||
return false; | ||
} | ||
|
||
n -= required; | ||
} | ||
|
||
true | ||
} | ||
|
||
pub fn minimized_maximum(n: i32, quantities: Vec<i32>) -> i32 { | ||
let n = n as u32; | ||
let quantities = quantities.as_slice(); | ||
let mut left = 1; | ||
let mut right = quantities.iter().map(|&x| x as u32).max().unwrap(); | ||
|
||
while left < right { | ||
let middle = (left + right) / 2; | ||
|
||
if Self::check(n, quantities, NonZeroU32::new(middle).unwrap()) { | ||
right = middle; | ||
} else { | ||
left = middle + 1; | ||
} | ||
} | ||
|
||
left as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimized_maximum(n: i32, quantities: Vec<i32>) -> i32 { | ||
Self::minimized_maximum(n, quantities) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/problem_2064_minimized_maximum_of_products_distributed_to_any_store/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 binary_search; | ||
|
||
pub trait Solution { | ||
fn minimized_maximum(n: i32, quantities: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((6, &[11, 6] as &[_]), 3), | ||
((7, &[15, 10, 10]), 5), | ||
((1, &[100_000]), 100_000), | ||
]; | ||
|
||
for ((n, quantities), expected) in test_cases { | ||
assert_eq!(S::minimized_maximum(n, quantities.to_vec()), expected); | ||
} | ||
} | ||
} |