-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathminimum_number_of_days_to_make_m_bouquets.cpp
45 lines (40 loc) · 1.25 KB
/
minimum_number_of_days_to_make_m_bouquets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// problem link: https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
// video explanation on my youtube channel: https://www.youtube.com/watch?v=KM4vZQg8jSs
bool m_bouquets_possible(vector<int>& bloomDay, int m, int k, int mid)
{
int n = bloomDay.size();
int flower=0, bouquet=0;
for(int i=0; i<n; i++)
{
if(bloomDay[i] <= mid) flower++;
else flower=0;
if(flower >= k)
{
bouquet++;
flower=0;
}
}
if(bouquet >= m) return true;
return false;
}
int minDays(vector<int>& bloomDay, int m, int k)
{
int left = *min_element(bloomDay.begin(),bloomDay.end());
int right = *max_element(bloomDay.begin(),bloomDay.end());
int result=-1;
if(bloomDay.size() < m*k) return -1;
while(left <= right)
{
int mid = (left+right)/2;
if(m_bouquets_possible(bloomDay, m, k, mid))
{
result = mid;
right = mid-1;
}
else
{
left = mid+1;
}
}
return result;
}