Skip to content

Commit 1128ec6

Browse files
committed
LC 2064. Minimized Maximum of Products Distributed to Any Store (Rust)
1 parent 22e4e63 commit 1128ec6

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ to the solution in this repository.
659659
| [2028. Find Missing Observations][lc2028] | 🟠 Medium | [![rust](res/rs.png)][lc2028rs] |
660660
| [2037. Minimum Number of Moves to Seat Everyone][lc2037] | 🟢 Easy | [![rust](res/rs.png)][lc2037rs] |
661661
| [2050. Parallel Courses III][lc2050] | 🔴 Hard | [![rust](res/rs.png)][lc2050rs] |
662+
| [2064. Minimized Maximum of Products Distributed to Any Store][lc2064] | 🟠 Medium | [![rust](res/rs.png)][lc2064rs] |
662663
| [2070. Most Beautiful Item for Each Query][lc2070] | 🟠 Medium | [![rust](res/rs.png)][lc2070rs] |
663664
| [2090. K Radius Subarray Averages][lc2090] | 🟠 Medium | [![rust](res/rs.png)][lc2090rs] |
664665
| [2092. Find All People With Secret][lc2092] | 🔴 Hard | [![rust](res/rs.png)][lc2092rs] |
@@ -2239,6 +2240,8 @@ to the solution in this repository.
22392240
[lc2037rs]: leetcode/minimum-number-of-moves-to-seat-everyone.rs
22402241
[lc2050]: https://leetcode.com/problems/parallel-courses-iii/
22412242
[lc2050rs]: leetcode/parallel-courses-iii.rs
2243+
[lc2064]: https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/
2244+
[lc2064rs]: leetcode/minimized-maximum-of-products-distributed-to-any-store.rs
22422245
[lc2070]: https://leetcode.com/problems/most-beautiful-item-for-each-query/
22432246
[lc2070rs]: leetcode/most-beautiful-item-for-each-query.rs
22442247
[lc2090]: https://leetcode.com/problems/k-radius-subarray-averages/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// 2064. Minimized Maximum of Products Distributed to Any Store
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/
5+
//
6+
// Tags: Array - Binary Search
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Use binary search to try the possible results and see if they work.
11+
///
12+
/// Time complexity: O(log(max(q))*m) - We binary search the result, for each try, we
13+
/// iterate over all quantities spreading them between stores.
14+
/// Space complexity: O(1) - We use an outer while loop with three usize pointers and an
15+
/// inner iterator plus fold.
16+
///
17+
/// Runtime 24 ms Beats 100%
18+
/// Memory 3.24 MB Beats 100%
19+
pub fn minimized_maximum(n: i32, quantities: Vec<i32>) -> i32 {
20+
let (mut left, mut right) = (1, *quantities.iter().max().unwrap());
21+
let mut mid;
22+
while left < right {
23+
mid = (left + right) / 2;
24+
// How many buckets do we need to spread this quantity at this spread rate?
25+
if quantities
26+
.iter()
27+
.fold(0, |acc, x| acc + ((x + mid - 1) / mid))
28+
> n
29+
{
30+
left = mid + 1;
31+
} else {
32+
right = mid;
33+
}
34+
}
35+
left as i32
36+
}
37+
}
38+
39+
// Tests.
40+
fn main() {
41+
let tests = [
42+
(6, vec![11, 6], 3),
43+
(7, vec![15, 10, 10], 5),
44+
(1, vec![100000], 100000),
45+
];
46+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
47+
let mut success = 0;
48+
for (i, t) in tests.iter().enumerate() {
49+
let res = Solution::minimized_maximum(t.0, t.1.clone());
50+
if res == t.2 {
51+
success += 1;
52+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
53+
} else {
54+
println!(
55+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
56+
i, t.2, res
57+
);
58+
}
59+
}
60+
println!();
61+
if success == tests.len() {
62+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
63+
} else if success == 0 {
64+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
65+
} else {
66+
println!(
67+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
68+
tests.len() - success
69+
)
70+
}
71+
}

0 commit comments

Comments
 (0)