-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinimumNumberOfDaysToMakeMBouquets.java
75 lines (68 loc) · 1.88 KB
/
MinimumNumberOfDaysToMakeMBouquets.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/*/
class Solution {
public int minDays(int[] bloomDay, int m, int k) {
int n = bloomDay.length, low = 0, high = 0, mid, result = -1;
for (int day : bloomDay)
high = Math.max(high,day);
while (low <= high)
{
mid = low+(high-low)/2;
if (possible(bloomDay,mid,m,k,n))
{
result = mid;
high = mid-1;
}
else low = mid+1;
}
return result;
}
public boolean possible(int[] bloomDay, int day, int m, int k, int n)
{
int consec = 0, bouquets = 0;
for (int i = 0; i < n; ++i)
{
if (bloomDay[i] <= day) ++consec;
else consec = 0;
if (consec == k)
{
++bouquets;
consec = 0;
}
}
if (consec == k)
++bouquets;
return bouquets >= m;
}
}
class Solution {
int m,k;
public int minDays(int[] bloomDay, int m, int k) {
if(m*k > bloomDay.length) return -1;
int max = 0;
this.m = m;
this.k=k;
for(int v:bloomDay) max = Math.max(max,v);
return binarySearch(bloomDay,1,max);
}
public int binarySearch(int[] a, int low,int high){
while(low<high){
int mid = (low+high)/2;
if(isPossible(a,mid)) high = mid;
else low = mid+1;
}
return low;
}
public boolean isPossible(int[] a,int val){
int low = 0;
int ans = 0;
for(int i=0;i<a.length;i++){
if(a[i] > val){
ans+= ((i-low)/k);
if(ans == m) return true;
low=i+1;
}
}
ans+= ((a.length-low)/k);
return (ans>=m);
}
}