You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2100-2199/2187.Minimum Time to Complete Trips/README_EN.md
+38-16
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,15 @@ So the minimum time needed to complete 1 trip is 2.
48
48
49
49
## Solutions
50
50
51
-
### Solution 1
51
+
### Solution 1: Binary Search
52
+
53
+
We notice that if we can complete at least $totalTrips$ trips in $t$ time, then we can also complete at least $totalTrips$ trips in $t' > t$ time. Therefore, we can use the method of binary search to find the smallest $t$.
54
+
55
+
We define the left boundary of the binary search as $l = 1$, and the right boundary as $r = \min(time) \times totalTrips$. For each binary search, we calculate the middle value $\text{mid} = \frac{l + r}{2}$, and then calculate the number of trips that can be completed in $\text{mid}$ time. If this number is greater than or equal to $totalTrips$, then we reduce the right boundary to $\text{mid}$, otherwise we increase the left boundary to $\text{mid} + 1$.
56
+
57
+
Finally, return the left boundary.
58
+
59
+
The time complexity is $O(n \times \log(m \times k))$, where $n$ and $k$ are the length of the array $time$ and $totalTrips$ respectively, and $m$ is the minimum value in the array $time$. The space complexity is $O(1)$.
52
60
53
61
<!-- tabs:start -->
54
62
@@ -91,15 +99,18 @@ class Solution {
91
99
public:
92
100
long long minimumTime(vector<int>& time, int totalTrips) {
93
101
int mi = *min_element(time.begin(), time.end());
94
-
long long left = 1, right = (long long) mi * totalTrips;
102
+
long long left = 1, right = 1LL * mi * totalTrips;
0 commit comments