File tree Expand file tree Collapse file tree 1 file changed +19
-12
lines changed
solution/1800-1899/1870.Minimum Speed to Arrive on Time Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Original file line number Diff line number Diff line change 1
1
func minSpeedOnTime (dist []int , hour float64 ) int {
2
+ if float64 (len (dist )) > math .Ceil (hour ) {
3
+ return - 1
4
+ }
5
+ const m int = 1e7
2
6
n := len (dist )
3
- const mx int = 1e7
4
- x := sort .Search (mx , func (s int ) bool {
5
- s ++
6
- var cost float64
7
- for _ , v := range dist [:n - 1 ] {
8
- cost += math .Ceil (float64 (v ) / float64 (s ))
7
+ ans := sort .Search (m + 1 , func (v int ) bool {
8
+ v ++
9
+ s := 0.0
10
+ for i , d := range dist {
11
+ t := float64 (d ) / float64 (v )
12
+ if i == n - 1 {
13
+ s += t
14
+ } else {
15
+ s += math .Ceil (t )
16
+ }
9
17
}
10
- cost += float64 (dist [n - 1 ]) / float64 (s )
11
- return cost <= hour
12
- })
13
- if x == mx {
18
+ return s <= hour
19
+ }) + 1
20
+ if ans > m {
14
21
return - 1
15
22
}
16
- return x + 1
17
- }
23
+ return ans
24
+ }
You can’t perform that action at this time.
0 commit comments