Skip to content

Commit 70701bb

Browse files
committed
Runtime: 14 ms (Top 100.0%) | Memory: 4.10 MB (Top 75.0%)
1 parent 1e7de57 commit 70701bb

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Runtime: 14 ms (Top 100.0%) | Memory: 4.10 MB (Top 75.0%)
2+
3+
impl Solution {
4+
pub fn min_days(bloom_day: Vec<i32>, m: i32, k: i32) -> i32 {
5+
let val = m as i64 * k as i64;
6+
let len = bloom_day.len() as i64;
7+
8+
if val > len {
9+
return -1;
10+
}
11+
12+
let (mut start, mut end) = Solution::min_max(&bloom_day);
13+
14+
15+
while start <= end {
16+
let mid = (start + end) / 2;
17+
if Solution::is_possible(&bloom_day, mid, m ,k) {
18+
end = mid - 1;
19+
} else {
20+
start = mid + 1;
21+
}
22+
}
23+
24+
25+
return start;
26+
}
27+
28+
fn min_max(bloom_day: &Vec<i32>) -> (i32, i32) {
29+
let (mut min, mut max) = (i32::MAX, 0);
30+
31+
for num in bloom_day {
32+
if *num < min {
33+
min = *num;
34+
}
35+
36+
if *num > max {
37+
max = *num;
38+
}
39+
}
40+
41+
42+
return (min, max);
43+
}
44+
45+
fn is_possible(bloom_day: &Vec<i32>, day: i32, m: i32, k: i32) -> bool {
46+
let (mut count, mut no_of_bouquets) = (0, 0);
47+
for num in bloom_day {
48+
if *num <= day {
49+
count += 1;
50+
} else {
51+
no_of_bouquets += (count / k) as i32;
52+
count = 0;
53+
}
54+
}
55+
56+
no_of_bouquets += (count / k) as i32;
57+
58+
if no_of_bouquets >= m {
59+
return true;
60+
}
61+
62+
return false;
63+
}
64+
}

0 commit comments

Comments
 (0)