Minimum days to make M bouquets

Binary Search On answers Medium
  • This problem, at its essence, involves optimal scheduling and resource allocation - concepts critical to many areas in software development
  • An interesting real-world application is found in distributed computing or microservices architecture, where you need to manage the execution time and ordering of numerous independent tasks (roses blooming) to meet certain requirements (making bouquets)
  • Similarly, this problem can reflect challenges in project management software, where tasks have specific start times (bloom days), and certain tasks have to be completed together (bouquet making) within specified time frames

Given n roses and an array nums where nums[i] denotes that the 'ith' rose will bloom on the nums[i]th day, only adjacent bloomed roses can be picked to make a bouquet. Exactly k adjacent bloomed roses are required to make a single bouquet. Find the minimum number of days required to make at least m bouquets, each containing k roses. Return -1 if it is not possible.

Examples:

Input: n = 8, nums = [7, 7, 7, 7, 13, 11, 12, 7], m = 2, k = 3

Output: 12

Explanation: On the 12th the first 4 flowers and the last 3 flowers would have already bloomed. So, we can easily make 2 bouquets, one with the first 3 and another with the last 3 flowers.

Input: n = 5, nums = [1, 10, 3, 10, 2], m = 3, k = 2

Output: -1

Explanation: If we want to make 3 bouquets of 2 flowers each, we need at least 6 flowers. But we are given only 5 flowers, so, we cannot make the bouquets.

Input: n = 5, nums = [1, 10, 3, 10, 2], m = 3, k = 1

Constraints

  •  1 <= n <= 105
  •  1 <= nums[i] <= 109
  •  1 <= m <= 106
  •  1 <= k <= n

Hints

  • "Use binary search to determine the smallest d within the range [1,max(nums)]. For each mid-point d, simulate the blooming process to check if at least m bouquets can be formed."
  • On a given day d, a rose is considered bloomed if nums[i]≤d. Traverse the array and count adjacent bloomed roses. If k adjacent roses are found, form one bouquet and reset the count. Repeat until m bouquets are formed or the array is exhausted.

Company Tags

Deloitte Johnson & Johnson Wayfair Walmart Medtronic Epic Games Siemens Healthineers Twilio Robinhood Rakuten Intel Philips Healthcare Flipkart Chewy Lyft Micron Technology Zomato Roblox Optum Qualcomm GE Healthcare DoorDash Zoho eBay OYO Rooms Google Microsoft Amazon Meta Apple Netflix Adobe