-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1482-Minimum_Number_of_Days_to_Make_m_Bouquets.cpp
More file actions
133 lines (121 loc) · 3.01 KB
/
1482-Minimum_Number_of_Days_to_Make_m_Bouquets.cpp
File metadata and controls
133 lines (121 loc) · 3.01 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*******************************************************************************
* 1482-Minimum_Number_of_Days_to_Make_m_Bouquets.cpp
* Billy.Ljm
* 19 June 2024
*
* =======
* Problem
* =======
* https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
*
* You are given an integer array `bloomDay`, an integer `m` and an integer `k`.
*
* You want to make m bouquets. To make a bouquet, you need to use k adjacent
* flowers from the garden.
*
* The garden consists of n flowers, the ith flower will bloom in the
* bloomDay[i] and then can be used in exactly one bouquet.
*
* Return the minimum number of days you need to wait to be able to make m
* bouquets from the garden. If it is impossible to make m bouquets return -1.
*
* ===========
* My Approach
* ===========
* The numbers of flowers in bloom and thus the number of bouquets we can make
* increases monotonically. Thus, we can just binary search to find the minimum
* days to make m bouquets.
*
* This has a time complexity of O(n log m) and space complexity of O(1), where
* n is the length of the argument `bloomDay` and m is the argument `m`
******************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Solution
*/
class Solution {
public:
int minDays(vector<int>& bloomDay, int m, int k) {
int min, max, mid; // for binary search
int nflowers, nwreaths; // to calculate wreaths
// see if solution possible
if (1L * m * k > bloomDay.size()) {
return -1;
}
// binary search
min = 0;
max = *max_element(bloomDay.begin(), bloomDay.end());
while (max > min) {
mid = (min + max) / 2;
// count number of wreaths
nflowers = 0;
nwreaths = 0;
for (int bloom : bloomDay) {
if (mid >= bloom) {
nflowers++;
if (nflowers >= k) {
nwreaths++;
nflowers = 0;
}
if (nwreaths >= m) {
break;
}
}
else {
nflowers = 0;
}
}
// adjust binary search
if (nwreaths < m) {
min = mid + 1;
}
else {
max = mid;
}
}
return min;
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
int m, k;
vector<int> bloomDay;
// test case 1
m = 3;
k = 1;
bloomDay = { 1,10,3,10,2 };
std::cout << "minDays(" << bloomDay << ", " << m << ", " << k << ") = ";
std::cout << sol.minDays(bloomDay, m, k) << std::endl;
// test case 2
m = 3;
k = 2;
bloomDay = { 1,10,3,10,2 };
std::cout << "minDays(" << bloomDay << ", " << m << ", " << k << ") = ";
std::cout << sol.minDays(bloomDay, m, k) << std::endl;
// test case 3
m = 2;
k = 3;
bloomDay = { 7,7,7,7,12,7,7 };
std::cout << "minDays(" << bloomDay << ", " << m << ", " << k << ") = ";
std::cout << sol.minDays(bloomDay, m, k) << std::endl;
return 0;
}