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