We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Minimum Time Required
- Discussions
Minimum Time Required
Minimum Time Required
Sort by
recency
|
152 Discussions
|
Please Login in order to post a comment
The binary search is certainly a great idea, but solutions I see all start from 0 or 1. The minum days is fastest-machine/number-of-machines * item_targets and the maxium days is slowest-machine/number-of-machines * item_targets:
JS binary search
JS Solution
If
minDays += 1
instead ofminDays = midDays + 1
is used, the time complexity would exceed the limit. Can anyone explain why?this is my solution, it's a bit complicated but it works =).
Explanation: Wouldn't it be great if we had the number of products produced for each day so, we could select the MINIMUM day which would produce our goal count? But we don't. So, do we go ahead and start calculating the number of products produced each day? No, because time complexity would suffer. Instead, we use binary search.
The basic logic is that we set a minimum day (lower bound) and a maximum day (upper bound, which would be the time taken by the slowest machine to produce the goal count). Then, we find the "mid" value between these 2 days and see the number of product produced in the "mid" day. If the value is lesser than the goal count, we search for our day between "mid" to maximum day. If the value is greater than the goal count, we search for our day between minimum to "mid" day.
Example: (because I got stuck a lot because of small implementation details) machines[] = [4L, 5L, 6L] goal = 12
----begin execution------------
minimumDays = 0 maximumDays = 6 * 12 = 72
So, our search bound is 0(minimumDays)-----------------------72(maximumDays)
Iteration 1: 0(minimumDays)---------36(mid)--------------72(maximumDays) prodCount = 22 > goal
Iteration 2: 0(minimumDays)-------------18(mid)-----------36(maximumDays) prodCount = 10 < goal
Iteration 3: 19(minimumDays)-------------27(mid)-----------36(maximumDays) prodCount = 15 > goal
Iteration 5: 19(minimumDays)-------------21(mid)-----------23(maximumDays) prodCount = 12 == goal (Huh, so this one is lesser than the day we found in the last iteration)
Iteration 6: 19(minimumDays)-------------20(mid)-----------21(maximumDays) prodCount = 12 == goal (even lesser!)
Iteration 7: 19(minimumDays, mid)-------------------------20(maximumDays) prodCount = 10 < goal
Iteration 8: * 20(minimumDays, maximumDays) * minimumDays == maximumDays, so loop ends. So we return the value from iteration 6! * * */