File tree Expand file tree Collapse file tree 1 file changed +18
-13
lines changed
solution/1800-1899/1870.Minimum Speed to Arrive on Time Expand file tree Collapse file tree 1 file changed +18
-13
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
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 ;
6
10
if (check (dist , mid , hour )) {
7
- right = mid ;
11
+ r = mid ;
8
12
} else {
9
- left = mid + 1 ;
13
+ l = mid + 1 ;
10
14
}
11
15
}
12
- return check ( dist , left , hour ) ? left : - 1 ;
16
+ return l > m ? - 1 : l ;
13
17
}
14
18
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 );
20
25
}
21
- return res <= hour ;
26
+ return s <= hour ;
22
27
}
23
- }
28
+ }
You can’t perform that action at this time.
0 commit comments