-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2187-minimum-time-to-complete-trips.js
More file actions
39 lines (35 loc) · 1.13 KB
/
2187-minimum-time-to-complete-trips.js
File metadata and controls
39 lines (35 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 2187. Minimum Time to Complete Trips
* https://leetcode.com/problems/minimum-time-to-complete-trips/
* Difficulty: Medium
*
* You are given an array time where time[i] denotes the time taken by the ith bus to complete
* one trip.
*
* Each bus can make multiple trips successively; that is, the next trip can start immediately
* after completing the current trip. Also, each bus operates independently; that is, the trips
* of one bus do not influence the trips of any other bus.
*
* You are also given an integer totalTrips, which denotes the number of trips all buses should
* make in total. Return the minimum time required for all buses to complete at least totalTrips
* trips.
*/
/**
* @param {number[]} time
* @param {number} totalTrips
* @return {number}
*/
var minimumTime = function(time, totalTrips) {
let left = 1;
let right = Math.min(...time) * totalTrips;
while (left < right) {
const mid = Math.floor((left + right) / 2);
const trips = time.reduce((sum, t) => sum + Math.floor(mid / t), 0);
if (trips >= totalTrips) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};