classSolution: defminDays(self, bloomDay: List[int], m: int, k: int) -> int: defcheck(x): state = [False] * len(bloomDay) for i inrange(len(bloomDay)): if bloomDay[i]-x<=0: state[i] = True # print(state) ans,ii,k_test = 0,0,0 # 以下两个while循环嵌套,会超时,目标是用一个循环解决。 # while ii<len(state): # while ii<len(state) and state[ii]==True: # k_test += 1 # ii += 1 # if k_test==k: # ans += 1 # k_test = 0 # if ii<len(state) and state[ii]==False: # ii += 1 # k_test = 0 # print(x,ans,ans >= m) # return ans >= m for ii inrange(len(state)): if state[ii]==True: k_test += 1 if k_test==k: ans += 1 k_test = 0 if state[ii]==False: k_test = 0 # print(x,ans,ans >= m) return ans >= m defsearch_II(check): # 二分法找最小等待天数 iflen(bloomDay)<m*k: print("全部开花也无法满足") return -1 left = 1 right = max(bloomDay) while left<=right: mid = (left+right)>>1 if check(mid): right = mid-1 else: left = mid+1 return left return search_II(check)