![]() |
VOOZH | about |
Geek is organizing a party at his house. For the party, he needs exactly n donuts for the guests. Geek decides to order the donuts from a nearby restaurant, which has l chefs and each chef has a rank r. A chef with rank r can make 1 donut in the first r minutes, 1 more donut in next 2r minutes, 1 more donut in 3r minutes, and so on. find the minimum time in which all the orders will be fulfilled.
Examples:
Input: n = 10, rank[] = [1, 2, 3, 4]
Output: 12
Explanation: Chef with rank 1, can make 4 donuts in time 1 + 2 + 3 + 4 = 10 mins
Chef with rank 2, can make 3 donuts in time 2 + 4 + 6 = 12 mins
Chef with rank 3, can make 2 donuts in time 3 + 6 = 9 mins
Chef with rank 4, can make 1 donuts in time = 4 minutes
Total donuts = 4 + 3 + 2 + 1 = 10 and
Maximum time taken by all = 12 minutes.Input: n = 8, rank[] = [1, 1, 1, 1, 1, 1, 1, 1]
Output: 1
Explanation: As all chefs are ranked 1, so each chef can make 1 donuts 1 min.
Total donuts = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 8 and min time = 1 minute
The idea is to use a brute-force linear search from time 1 up to Integer Maximum to check at each time point whether it is possible to make at least n donuts. At each time point, find the maximum number of donuts each chef can make and compare if the sum of donuts is greater than equal to n. The first time for which this is possible is considered the minimum time to fulfill the order.
12
Time Complexity: O(t Γ l Γ sqrt(t)) where t is the maximum time iterated, l is the number of cooks. For each time point and each chef, the number of donuts k is found using 1 Γ r + 2 Γ r + 3 Γ r + ... + k Γ r <= timeAlloted. Maximum iterations will be sqrt(2 Γ timeAlloted / r)) which is roughly sqrt(timeAlloted).
Auxiliary Space: O(1)
The idea is to use binary search on the time to find the minimum time required to produce at least n donuts. For any given time, we calculate how many donuts each chef can make by summing the terms of an arithmetic progression where each chef takes r, 2r, 3r, ... seconds per donut, with r being the chef's rank. The total number of donuts made is the sum of such APs across all chefs. If the total meets or exceeds n, we try to minimize the time further; otherwise, we increase it. This combination of AP sum logic and binary search helps efficiently reach the answer.
Step by step approach:
12
Time Complexity: O(l Γ log(n)), where l is the length of ranks array, and n is the number of donuts to be made.
Auxiliary Space: O(1).