Skip to content

Commit f33c9ef

Browse files
authored
Update Solution.go
1 parent fc96647 commit f33c9ef

File tree

1 file changed

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

1 file changed

+19
-12
lines changed
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
func minSpeedOnTime(dist []int, hour float64) int {
2+
if float64(len(dist)) > math.Ceil(hour) {
3+
return -1
4+
}
5+
const m int = 1e7
26
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+
}
917
}
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 {
1421
return -1
1522
}
16-
return x + 1
17-
}
23+
return ans
24+
}

0 commit comments

Comments
 (0)