-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeWork05_mini_num_bouquets.cpp
More file actions
68 lines (55 loc) · 1.61 KB
/
HomeWork05_mini_num_bouquets.cpp
File metadata and controls
68 lines (55 loc) · 1.61 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
class Solution {
public:
int minDays(vector<int>& bloomDay, int m, int k) {
int result = 0;
if(m*k > bloomDay.size()) return -1;
//假设 第n天恰好能制作m朵花
// n --> 增大 则 m 增大
//| ___/
//| __/
//|/___
// n 最大值为 max(bloomDay)
// n 最小值为 min(bloomDay)
// m 的目标值为 M
// 限定条件:相邻的 k朵
int min_Day = 1000000000;
int max_Day = 0;
for(int i : bloomDay){
min_Day = min(min_Day,i);
max_Day = max(max_Day,i);
}
if(m*k == bloomDay.size()) return max_Day;
int left = min_Day;
int right = max_Day;
int target = m;
while(left < right){
int mid = (left + right)>>1;
if(can_finsh_flowers(bloomDay,mid,k) >= target){
right = mid;
}
else{
left = mid + 1 ;
}
}
return right;
//can_finsh_flowers(bloomDay,3,2,1);
//return result;
}
int can_finsh_flowers(vector<int>& bloomDay, int day_passed,int need_flowers){
int flowers_we_have = 0;
int bunch_we_have = 0;
for(int i: bloomDay){
if(i <= day_passed){
++flowers_we_have;
if(flowers_we_have >= need_flowers){
++bunch_we_have;
flowers_we_have = 0;
}
}
else{
flowers_we_have = 0;
}
}
return bunch_we_have;
}
};