leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1482.cpp (736B)
0 class Solution { 1 static bool possible(const vector<int> &bloomDay, int m, int k, int n) { 2 int adj = 0; 3 4 for (const auto day : bloomDay) { 5 adj = day <= n ? adj + 1 : 0; 6 if (adj == k) adj = 0, m--; 7 } 8 9 return m <= 0; 10 } 11 12 public: 13 int minDays(const vector<int> &bloomDay, int m, int k) const { 14 if ((long long)m * k > size(bloomDay)) return -1; 15 16 int low = 1, high = *max_element(begin(bloomDay), end(bloomDay)); 17 18 while (low < high) { 19 const int mid = low + (high - low) / 2; 20 if (possible(bloomDay, m, k, mid)) 21 high = mid; 22 else 23 low = mid + 1; 24 } 25 26 return low; 27 } 28 };