Skip to content

Commit c2c9a4a

Browse files
authored
Update Solution.java
1 parent 62613e3 commit c2c9a4a

File tree

1 file changed

+18
-13
lines changed
  • solution/1800-1899/1870.Minimum Speed to Arrive on Time

1 file changed

+18
-13
lines changed
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
class Solution {
22
public int minSpeedOnTime(int[] dist, double hour) {
3-
int left = 1, right = (int) 1e7;
4-
while (left < right) {
5-
int mid = (left + right) >> 1;
3+
if (dist.length > Math.ceil(hour)) {
4+
return -1;
5+
}
6+
final int m = (int) 1e7;
7+
int l = 1, r = m + 1;
8+
while (l < r) {
9+
int mid = (l + r) >> 1;
610
if (check(dist, mid, hour)) {
7-
right = mid;
11+
r = mid;
812
} else {
9-
left = mid + 1;
13+
l = mid + 1;
1014
}
1115
}
12-
return check(dist, left, hour) ? left : -1;
16+
return l > m ? -1 : l;
1317
}
1418

15-
private boolean check(int[] dist, int speed, double hour) {
16-
double res = 0;
17-
for (int i = 0; i < dist.length; ++i) {
18-
double cost = dist[i] * 1.0 / speed;
19-
res += (i == dist.length - 1 ? cost : Math.ceil(cost));
19+
private boolean check(int[] dist, int v, double hour) {
20+
double s = 0;
21+
int n = dist.length;
22+
for (int i = 0; i < n; ++i) {
23+
double t = dist[i] * 1.0 / v;
24+
s += i == n - 1 ? t : Math.ceil(t);
2025
}
21-
return res <= hour;
26+
return s <= hour;
2227
}
23-
}
28+
}

0 commit comments

Comments
 (0)