Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 1.21 KB

1482.-zhi-zuo-m-shu-hua-suo-xu-de-zui-shao-tian-shu.md

File metadata and controls

51 lines (37 loc) · 1.21 KB

1482. 制作 m 束花所需的最少天数

给你一个整数数组 bloomDay,以及两个整数 m 和 k 。

现需要制作 m 束花。制作花束时,需要使用花园中 相邻的 k 朵花 。

花园中有 n 朵花,第 i 朵花会在 bloomDay[i] 时盛开,恰好 可以用于 一束 花中。

请你返回从花园中摘 m 束花需要等待的最少的天数。如果不能摘到 m 束花则返回 -1 。

class Solution:
    def minDays(self, bloomDay: List[int], m: int, k: int) -> int:

        ## 周赛原题 
        ## 二分
        if m * k > len(bloomDay):
            return -1
        
        l = 1
        r = 1000000000
        def check(mid):
            tmp = 0
            cnt = 1 if bloomDay[0] <= mid else 0
            for i in range(1, len(bloomDay)):
                if bloomDay[i] <= mid:
                    cnt += 1
                else:
                    tmp += (cnt // k)
                    cnt = 0
            tmp += (cnt // k)
            # print(tmp)
            return tmp >= m

        while l < r :
            mid = l + r >> 1
            if check(mid):
                r = mid
            else:
                l = mid + 1

        return l