classSolution: defminDays(self, bloomDay: List[int], m: int, k: int) -> int: # check if we can make m bouquets with k flowers in d days defcheck(d: int): res = cur = 0# res: number of bouquets, cur: number of adjacent flowers for bloom in bloomDay: if bloom <= d: cur += 1 if cur == k: res += 1 cur = 0 else: # not adjacent cur = 0 return res >= m left, right = min(bloomDay), max(bloomDay) while left <= right: mid = (left + right) // 2 if check(mid): right = mid - 1 else: left = mid + 1 return left if left <= max(bloomDay) else -1
classSolution { public: intminDays(vector<int>& bloomDay, int m, int k){ // check if we can make m bouquets with k flowers in d days auto check = [&](int d) { int res = 0, cur = 0; for (int bloom : bloomDay) { if (bloom <= d) { cur++; if (cur == k) { res++; cur = 0; } } else { // not adjacent cur = 0; } } return res >= m; };
int mx = INT_MIN, mn = INT_MAX; for (int bloom : bloomDay) { mx = max(mx, bloom); mn = min(mn, bloom); } int left = mn, right = mx; while (left <= right) { int mid = left + (right - left) / 2; if (check(mid)) { right = mid - 1; } else { left = mid + 1; } }
return left <= mx ? left : -1; } };
寫在最後
Cover photo is generated by @たろたろ, thanks for their work!