-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
83 lines (65 loc) · 1.48 KB
/
Copy pathSolution.java
File metadata and controls
83 lines (65 loc) · 1.48 KB
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
76
77
78
79
80
81
82
83
class Solution
{
public int minDays(int[] bloomDay, int m, int k)
{
if (m * k > bloomDay.length)
{
return -1;
}
int min = Integer.MAX_VALUE;
int max = 0;
for (int day : bloomDay)
{
min = Math.min(min, day);
max = Math.max(max, day);
}
int l = min, r = max;
while (l < r)
{
int mid = (l + r) / 2;
int numberOfBoqu = _calculate(bloomDay, k, mid);
if (numberOfBoqu >= m)
{
r = mid;
}
else
{
l = mid + 1;
}
}
return r;
}
private int _calculate(int[] bloomDay, int k, int mid)
{
int n = bloomDay.length;
boolean[] canBeTaken = new boolean[n];
for (int i = 0; i < n; i++)
{
if (bloomDay[i] <= mid)
{
canBeTaken[i] = true;
}
}
int count = 0;
int i = 0;
while (i < n)
{
if (!canBeTaken[i])
{
i++;
continue;
}
int numberOfFlowers = 0;
while (numberOfFlowers < k && i < n && canBeTaken[i])
{
i++;
numberOfFlowers++;
}
if (numberOfFlowers == k)
{
count++;
}
}
return count;
}
}