Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 1482 · Binary Search on Answer

Minimum Number of Days to Make m Bouquets

LeetCode 1482 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Bouquets(day) is monotonic ⇒ binary search the day.

நடுத்தரம்ArrayBinary Search
LeetCode இல் திற ↗

நான் Minimum Number of Days to Make m Bouquets (LeetCode 1482) ஐ இப்படித் தீர்த்தேன் — Binary Search on Answer பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Once a flower blooms it stays bloomed, so the number of bouquets you can make only grows with the day. Binary search the earliest day on which you can collect m groups of k adjacent bloomed flowers.

Approach

  1. 1

    Impossible if m·k exceeds the number of flowers.

  2. 2

    For a day, scan and count runs of bloomed flowers; every k in a row makes a bouquet.

  3. 3

    Enough bouquets → search earlier; else later.

நான் எப்படித் தீர்த்தேன்

Binary search on daysPythonO(n log D) time
#leetcode editorial
"""
we observe a crucial property: once a flower blooms, it remains bloomed. This means that the number of bloomed flowers stays the same or increases as the days progress. The same goes for the number of bouquets that can possibly be made.
"""
class Solution:
    def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
        if len(bloomDay)<m*k:
            return -1
        def getNOfBouquets(mid):
            bouquets=0
            count=0
            for day in bloomDay: #n
                #if the flower was already blosommed or blosomosed on the day we can add count
                if day<=mid:
                    count+=1
                else:
                    count=0
                if count==k: #consequitive days
                    bouquets+=1
                    count=0
            return bouquets

        low = 1 #day 10
        high = max(bloomDay) #max day
        while(low<=high):
            mid = (low+high)//2 #logD
            noofBouquets=getNOfBouquets(mid)
            if noofBouquets>=m:
                high= mid-1 #look for min days
            else:
                low = mid+1
        return low

ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Minimum Number of Days to Make m Bouquets — அடிக்கடி கேட்கப்படும் கேள்விகள்

Minimum Number of Days to Make m Bouquets பின்னாலுள்ள intuition என்ன?

Once a flower blooms it stays bloomed, so the number of bouquets you can make only grows with the day. Binary search the earliest day on which you can collect m groups of k adjacent bloomed flowers.

Minimum Number of Days to Make m Bouquets (LeetCode 1482) ஐ எப்படித் தீர்ப்பது?

1. Impossible if m·k exceeds the number of flowers. 2. For a day, scan and count runs of bloomed flowers; every k in a row makes a bouquet. 3. Enough bouquets → search earlier; else later.

Minimum Number of Days to Make m Bouquets இன் time மற்றும் space complexity என்ன?

Time: O(n log D). Space: O(1). (Accepted approach: Binary search on days.)

Minimum Number of Days to Make m Bouquets இல் பொதுவான தவறுகள் என்ன?

Reset the run counter after forming a bouquet and on unbloomed flowers.