Minimum Time Required

  • + 0 comments

    JS binary search

    function minTime(machines, goal) {
      let min = 1, max = goal * Math.max(...machines)
    
      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
    }