Minimum Time to Complete Trips LeetCode solution in JavaScript

1
var minimumTime = function (time, totalTrips) {
2
let low = 1;
3
let high = Number.MAX_SAFE_INTEGER;
4
let ans = 0;
5

6
while (low <= high) {
7
let mid = Math.floor(low + (high - low) / 2); // to prevent overflow
8

9
if (isPossible(time, mid, totalTrips)) {
10
ans = mid;
11
high = mid - 1;
12
} else {
13
low = mid + 1;
14
}
15
}
16
return ans;
17
};
18

19
function isPossible(arr, mid, totalTrips) {
20
let trips = 0;
21
for (let i = 0; i < arr.length; i++) {
22
trips += Math.floor(mid / arr[i]);
23
}
24
return trips >= totalTrips;
25
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0