Skip to content

Commit

Permalink
Added minimized maximum solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Nov 14, 2024
1 parent 836df3d commit 685e214
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ programming challenges completed in other languages.
- Most Beautiful Item for Each Query (https://leetcode.com/problems/most-beautiful-item-for-each-query)
- Count Number of Fair Pairs(https://leetcode.com/problems/count-the-number-of-fair-pairs)
- Sum of Square Numbers (https://leetcode.com/problems/sum-of-square-numbers)
_ Minimized Maximum of Products Distributed to Any Store (https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store)

#### Hard

Expand Down
22 changes: 22 additions & 0 deletions src/leetcode/minimized_maximum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub fn minimized_maximum(n: i32, quantities: Vec<i32>) -> i32 {
let (mut left, mut right) = (1, 100_000);
while left < right {
let mid = (left + right) / 2;
let sum = quantities.iter().map(|q| (q + mid - 1) / mid).sum::<i32>();
match sum > n {
true => left = mid + 1,
false => right = mid,
}
}
left
}

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

#[test]
fn finds_the_minimized_maximum_products_per_store() {
assert_eq!(3, minimized_maximum(6, vec![11, 6]))
}
}
1 change: 1 addition & 0 deletions src/leetcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ mod duplicate_file;
mod max_beauty;
mod count_fair_pairs;
mod square_numbers;
mod minimized_maximum;

0 comments on commit 685e214

Please sign in to comment.