LeetCode 1482. Minimum Number of Days to Make m Bouquets (javascript solution)

Description:

Given an integer array bloomDay, an integer m and an integer k.

We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.

The garden consists of n flowers, the ith flower will bloom in …


This content originally appeared on DEV Community and was authored by codingpineapple

Description:

Given an integer array bloomDay, an integer m and an integer k.

We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.

The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.

Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.

Solution:

Time Complexity : O(nlog(n))
Space Complexity: O(1)

// Binary Search
var minDays = function(bloomDay, m, k) {
    // Check if we can make m bouquets given in day days
    function checkCondition(day) {
        let bouq = 0
        let flowers = 0
        for(const bloom of bloomDay) {
            if(day >= bloom) flowers++
            else flowers = 0
            if(flowers===k) {
                bouq++
                flowers=0
            }
        }
        // If we can make m or more bouquets, check if we can do it faster
        return bouq >= m 
    }

    // It is impossible to make m bouquets if we don't have enough flowers
    if(m * k > bloomDay.length) return -1
    // The fastest we can make a bouquet is the min of bloomDay and the slowest we can make a bouquet is the max of bloomDay
    let left = Math.min(...bloomDay), right = Math.max(...bloomDay)

    // Binary search template
    while(left < right) {
        const mid = left + Math.floor((right-left)/2)
        if(checkCondition(mid)) {
            right = mid
        } else {
            left = mid + 1
        }
    }
    return left
};


This content originally appeared on DEV Community and was authored by codingpineapple


Print Share Comment Cite Upload Translate Updates
APA

codingpineapple | Sciencx (2021-05-14T21:44:15+00:00) LeetCode 1482. Minimum Number of Days to Make m Bouquets (javascript solution). Retrieved from https://www.scien.cx/2021/05/14/leetcode-1482-minimum-number-of-days-to-make-m-bouquets-javascript-solution/

MLA
" » LeetCode 1482. Minimum Number of Days to Make m Bouquets (javascript solution)." codingpineapple | Sciencx - Friday May 14, 2021, https://www.scien.cx/2021/05/14/leetcode-1482-minimum-number-of-days-to-make-m-bouquets-javascript-solution/
HARVARD
codingpineapple | Sciencx Friday May 14, 2021 » LeetCode 1482. Minimum Number of Days to Make m Bouquets (javascript solution)., viewed ,<https://www.scien.cx/2021/05/14/leetcode-1482-minimum-number-of-days-to-make-m-bouquets-javascript-solution/>
VANCOUVER
codingpineapple | Sciencx - » LeetCode 1482. Minimum Number of Days to Make m Bouquets (javascript solution). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/14/leetcode-1482-minimum-number-of-days-to-make-m-bouquets-javascript-solution/
CHICAGO
" » LeetCode 1482. Minimum Number of Days to Make m Bouquets (javascript solution)." codingpineapple | Sciencx - Accessed . https://www.scien.cx/2021/05/14/leetcode-1482-minimum-number-of-days-to-make-m-bouquets-javascript-solution/
IEEE
" » LeetCode 1482. Minimum Number of Days to Make m Bouquets (javascript solution)." codingpineapple | Sciencx [Online]. Available: https://www.scien.cx/2021/05/14/leetcode-1482-minimum-number-of-days-to-make-m-bouquets-javascript-solution/. [Accessed: ]
rf:citation
» LeetCode 1482. Minimum Number of Days to Make m Bouquets (javascript solution) | codingpineapple | Sciencx | https://www.scien.cx/2021/05/14/leetcode-1482-minimum-number-of-days-to-make-m-bouquets-javascript-solution/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.