
You are given the number of machines 'N', an array 'M' of length 'N' where 'M[i]' represents the number of days the i-th machine takes to produce one item, and the total number of items 'O' that need to be ordered.
Return the minimum number of days required for all machines working in parallel to complete the order.
Let 'N' = 2, 'M' = [3, 1], 'O' = 5.
Day 1: Machine 2 produces an item
Day 2: Machine 2 produces an item
Day 3: Both Machine 1 and Machine 2 produce an item
Day 4: Machine 2 produces an item
Total items produced = 1 + 1 + 2 + 1 = 5 items.
Therefore, the answer is 4 days.
The first line contains three integers, 'N', 'O'.
The second line contains 'N' integers representing the array 'M'.
Output Format :
Return the minimum number of days required to complete the order.
Note :
You don’t need to print anything. Just implement the given function.
1 <= 'N' <= 10^5
1 <= 'M[i]' <= 10^9
1 <= 'O' <= 10^9
Time Limit: 1 sec
2 7
4 5
14
There are 2 machines. Machine 1 takes 4 days per item and Machine 2 takes 5 days per item. We need to produce a total of 7 items.
In 14 days:
Machine 1 can produce 14 / 4 = 3 items.
Machine 2 can produce 14 / 5 = 2 items.
Total items produced = 3 + 2 = 5 items. This is not enough. Let's try 15 days.
In 15 days:
Machine 1 can produce 15 / 4 = 3 items.
Machine 2 can produce 15 / 5 = 3 items.
Total items produced = 3 + 3 = 6 items. Still not enough. Let's try 16 days.
In 16 days:
Machine 1 can produce 16 / 4 = 4 items.
Machine 2 can produce 16 / 5 = 3 items.
Total items produced = 4 + 3 = 7 items.
Thus, the minimum number of days required is 16.
4 15
1 1 1 1
4
The problem asks for the minimum number of days. The number of items produced is non-decreasing with the number of days. Which concept comes to mind?
Approach:
Algorithm:
O(N * log(max(M) * O)), where 'N' is the number of machines and 'O' is the number of items ordered, and 'max(M)' is the maximum production time of a machine.
The binary search runs for approximately log(max(M) * O) iterations. In each iteration, we iterate through all 'N' machines to calculate the total number of items produced. Thus, the overall time complexity is of the order O(N * log(max(M) * O)).
O(1).
We are only using a few variables to store the lower bound, upper bound, middle value, total produced items, and the answer. The space used does not depend on the input size. Thus, the overall space complexity is of the order O(1).