forked from USPCodeLabSanca/dev.hire-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1482.cpp
More file actions
48 lines (39 loc) · 1.13 KB
/
Copy path1482.cpp
File metadata and controls
48 lines (39 loc) · 1.13 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
class Solution {
public:
int minDays(vector<int>& bloomDay, int m, int k) {
int start = 1;
int end = bloomDay[0];
int currentMid = -1;
for (int i = 0; i < bloomDay.size(); i++) {
if (bloomDay[i] > end)
end = bloomDay[i];
}
while (start <= end) {
int mid = start + (end - start) / 2;
if (this->canMakeABouquete(bloomDay, mid, k, m)) {
end = mid - 1;
currentMid = mid;
} else {
start = mid + 1;
}
}
return currentMid;
}
bool canMakeABouquete(vector<int>& bloomDay, int day, int k, int m) {
int currentK = 0;
int mDid = 0;
for (int i = 0; i < bloomDay.size(); i++) {
if (day >= bloomDay[i])
currentK++;
else
currentK = 0;
if (currentK == k) {
mDid++;
currentK = 0;
}
}
if (mDid >= m)
return true;
return false;
}
};