- Minimum Time Required
- Discussions

# Minimum Time Required

# Minimum Time Required

+ 0 comments 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:

def min_time(machines, num_items_goal): fastest_machine = min(machines) slowest_machine = max(machines) fastest_production_num_days_per_item = fastest_machine // len(machines) slowest_production_num_days_per_item = slowest_machine // len(machines) + 1 min_days, max_days = \ fastest_production_num_days_per_item * num_items_goal, \ slowest_production_num_days_per_item * num_items_goal while max_days - min_days > 1: mid = (max_days + min_days) // 2 item_cnt = 0 for m in machines: item_cnt += mid // m if item_cnt < num_items_goal: min_days = mid else: max_days = mid return max_days # we return the bigger number

+ 0 comments **JS binary search**function minTime(machines, goal) { let min = 1, max = goal * machines.sort((a, b) => b - a)[0] while (min < max) { let mid = parseInt((min + max) / 2) let prod = machines.reduce((prev, curr) => prev + parseInt(mid / curr), 0) if (prod < goal) min = mid + 1 else max = mid } return min }

+ 0 comments JS Solution

If

`minDays += 1`

instead of`minDays = midDays + 1`

is used, the time complexity would exceed the limit. Can anyone explain why?function minTime(machines, goal) { let minDays = 0; let maxDays = Math.max(...machines) * goal; while (minDays < maxDays) { let midDays = Math.floor((minDays + maxDays) / 2); let totalItems = 0; for (let machine of machines) { totalItems += Math.floor(midDays / machine); } if (totalItems < goal) { minDays = midDays + 1; } else { maxDays = midDays; } } return minDays; } // console.log(minTime([2, 3, 2], 10)); // 8 // console.log(minTime([2, 3], 5)); // 6 // console.log(minTime([1, 3, 4], 10)); // 7 // console.log(minTime([4, 5, 6], 12)); // 20

+ 0 comments this is my solution, it's a bit complicated but it works =).

def minTime(machines, goal): sum_mach = sum(1/x for x in machines) days = [1, int(goal/sum_mach)] n = 1 print(days[-1]) while True: sum_prod = sum(days[-1]//x for x in machines) prom = ((goal-sum_prod)/2)**n; min_mac = min([x - (days[-1] % x) for x in machines]) * prom if sum_prod >= goal: if prom == 1: break else: days[-1] = days[-2] n = 0 else: days.append(days[-1]+min_mac) print(prom, sum_prod) return(int(days[-1]))

+ 0 comments 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 4:* 19(minimumDays)-------------23(mid)-----------27(maximumDays) prodCount = 12 == goal (Yay, we found the day that produces the goal! But wait. Could this be the actual MINIMUM day?)

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! * * */

Sort 152 Discussions, By:

Please Login in order to post a comment