Skip to content

Commit 9f44794

Browse files
committed
feat: update solutions to lc problem: No.1870
No.1870.Minimum Speed to Arrive on Time
1 parent ff7e015 commit 9f44794

File tree

4 files changed

+58
-79
lines changed

4 files changed

+58
-79
lines changed

solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md

+25-32
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,9 @@ class Solution:
136136
res += (d / speed) if i == len(dist) - 1 else math.ceil(d / speed)
137137
return res <= hour
138138

139-
left, right = 1, 10**7
140-
while left < right:
141-
mid = (left + right) >> 1
142-
if check(mid):
143-
right = mid
144-
else:
145-
left = mid + 1
146-
return left if check(left) else -1
139+
r = 10**7 + 1
140+
ans = bisect_left(range(1, r), True, key=check) + 1
141+
return -1 if ans == r else ans
147142
```
148143

149144
### **Java**
@@ -205,6 +200,28 @@ public:
205200
};
206201
```
207202

203+
### **Go**
204+
205+
```go
206+
func minSpeedOnTime(dist []int, hour float64) int {
207+
n := len(dist)
208+
const mx int = 1e7
209+
x := sort.Search(mx, func(s int) bool {
210+
s++
211+
var cost float64
212+
for _, v := range dist[:n-1] {
213+
cost += math.Ceil(float64(v) / float64(s))
214+
}
215+
cost += float64(dist[n-1]) / float64(s)
216+
return cost <= hour
217+
})
218+
if x == mx {
219+
return -1
220+
}
221+
return x + 1
222+
}
223+
```
224+
208225
### **JavaScript**
209226

210227
```js
@@ -242,30 +259,6 @@ function arriveOnTime(dist, speed, hour) {
242259
}
243260
```
244261

245-
### **Go**
246-
247-
```go
248-
func minSpeedOnTime(dist []int, hour float64) int {
249-
n := len(dist)
250-
const mx int = 1e7 + 1
251-
x := sort.Search(mx, func(s int) bool {
252-
if s == 0 {
253-
return false
254-
}
255-
var cost float64
256-
for _, v := range dist[:n-1] {
257-
cost += math.Ceil(float64(v) / float64(s))
258-
}
259-
cost += float64(dist[n-1]) / float64(s)
260-
return cost <= hour
261-
})
262-
if x < 0 || x == mx {
263-
return -1
264-
}
265-
return x
266-
}
267-
```
268-
269262
### **Rust**
270263

271264
```rust

solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md

+25-32
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,9 @@ class Solution:
113113
res += (d / speed) if i == len(dist) - 1 else math.ceil(d / speed)
114114
return res <= hour
115115

116-
left, right = 1, 10**7
117-
while left < right:
118-
mid = (left + right) >> 1
119-
if check(mid):
120-
right = mid
121-
else:
122-
left = mid + 1
123-
return left if check(left) else -1
116+
r = 10**7 + 1
117+
ans = bisect_left(range(1, r), True, key=check) + 1
118+
return -1 if ans == r else ans
124119
```
125120

126121
### **Java**
@@ -180,6 +175,28 @@ public:
180175
};
181176
```
182177

178+
### **Go**
179+
180+
```go
181+
func minSpeedOnTime(dist []int, hour float64) int {
182+
n := len(dist)
183+
const mx int = 1e7
184+
x := sort.Search(mx, func(s int) bool {
185+
s++
186+
var cost float64
187+
for _, v := range dist[:n-1] {
188+
cost += math.Ceil(float64(v) / float64(s))
189+
}
190+
cost += float64(dist[n-1]) / float64(s)
191+
return cost <= hour
192+
})
193+
if x == mx {
194+
return -1
195+
}
196+
return x + 1
197+
}
198+
```
199+
183200
### **JavaScript**
184201

185202
```js
@@ -217,30 +234,6 @@ function arriveOnTime(dist, speed, hour) {
217234
}
218235
```
219236

220-
### **Go**
221-
222-
```go
223-
func minSpeedOnTime(dist []int, hour float64) int {
224-
n := len(dist)
225-
const mx int = 1e7 + 1
226-
x := sort.Search(mx, func(s int) bool {
227-
if s == 0 {
228-
return false
229-
}
230-
var cost float64
231-
for _, v := range dist[:n-1] {
232-
cost += math.Ceil(float64(v) / float64(s))
233-
}
234-
cost += float64(dist[n-1]) / float64(s)
235-
return cost <= hour
236-
})
237-
if x < 0 || x == mx {
238-
return -1
239-
}
240-
return x
241-
}
242-
```
243-
244237
### **Rust**
245238

246239
```rust
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
func minSpeedOnTime(dist []int, hour float64) int {
22
n := len(dist)
3-
const mx int = 1e7 + 1
3+
const mx int = 1e7
44
x := sort.Search(mx, func(s int) bool {
5-
if s == 0 {
6-
return false
7-
}
5+
s++
86
var cost float64
97
for _, v := range dist[:n-1] {
108
cost += math.Ceil(float64(v) / float64(s))
119
}
1210
cost += float64(dist[n-1]) / float64(s)
1311
return cost <= hour
1412
})
15-
if x < 0 || x == mx {
13+
if x == mx {
1614
return -1
1715
}
18-
return x
16+
return x + 1
1917
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
class Solution:
22
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
3-
def arrive_on_time(speed):
3+
def check(speed):
44
res = 0
55
for i, d in enumerate(dist):
66
res += (d / speed) if i == len(dist) - 1 else math.ceil(d / speed)
77
return res <= hour
88

9-
left, right = 1, 10**7
10-
while left < right:
11-
mid = (left + right) >> 1
12-
if arrive_on_time(mid):
13-
right = mid
14-
else:
15-
left = mid + 1
16-
return left if arrive_on_time(left) else -1
9+
r = 10**7 + 1
10+
ans = bisect_left(range(1, r), True, key=check) + 1
11+
return -1 if ans == r else ans

0 commit comments

Comments
 (0)