Skip to content

Commit 1a1c8fd

Browse files
committed
Problem 1482
1 parent b26b9f6 commit 1a1c8fd

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Problem 1482: Minimum Number of Days to Make m Bouquets
3+
* Prompt: Given an integer array bloomDay, an integer m and an integer k.
4+
* We need to make m bouquets. To make a bouquet, you need to use k
5+
* adjacent flowers from the garden.
6+
* The garden consists of n flowers, the ith flower will bloom in the
7+
* bloomDay[i] and then can be used in exactly one bouquet.
8+
* Return the minimum number of days you need to wait to be able to make m
9+
* bouquets from the garden. If it is impossible to make m bouquets
10+
* return -1.
11+
* Date: 07/07/2020
12+
*/
13+
class Solution {
14+
public int minDays(int[] bloomDay, int m, int k) {
15+
int left = 1; int right = 1000000000;
16+
int len = bloomDay.length;
17+
if (len < m * k) return -1;
18+
while (left < right)
19+
{
20+
int mid = left + (right - left) / 2;
21+
int count = 0;
22+
int bouq = 0;
23+
for (int i = 0; i < len; ++i)
24+
{
25+
if (bloomDay[i] <= mid)
26+
count++;
27+
else
28+
{
29+
bouq += (count / k);
30+
count = 0;
31+
}
32+
}
33+
bouq += (count / k);
34+
if (bouq < m)
35+
left = mid + 1;
36+
else
37+
right = mid;
38+
}
39+
return left;
40+
}
41+
}
42+
43+
/**
44+
* Notes: Use binary search to find the best date. For each given date
45+
* we can calculate out how many bouque we can make by finding concecutive
46+
* flowers that bloomed and produce the caluclation. Then if we don't have
47+
* enough flowers we can go to the right interval or got to the left interval
48+
* for the other way around.
49+
*/

0 commit comments

Comments
 (0)