Skip to content

Commit 8b37dba

Browse files
committed
feat: add ts solution to lc problem: No.1870
1 parent a491049 commit 8b37dba

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,37 @@ function arriveOnTime(dist, speed, hour) {
310310
}
311311
```
312312

313+
#### TypeScript
314+
315+
```ts
316+
function minSpeedOnTime(dist: number[], hour: number): number {
317+
if (dist.length > Math.ceil(hour)) return -1;
318+
319+
const check = (speed: number) => {
320+
const n = dist.length;
321+
let time = 0;
322+
323+
for (let i = 0; i < n; i++) {
324+
const t = dist[i] / speed;
325+
time += i === n - 1 ? t : Math.ceil(t);
326+
}
327+
328+
return hour >= time;
329+
};
330+
331+
const max = 10 ** 7;
332+
let [l, r] = [1, max];
333+
334+
while (l <= r) {
335+
const i = (l + r) >> 1;
336+
if (check(i)) r = i - 1;
337+
else l = i + 1;
338+
}
339+
340+
return l <= max ? l : -1;
341+
}
342+
```
343+
313344
<!-- tabs:end -->
314345

315346
<!-- solution:end -->

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,37 @@ function arriveOnTime(dist, speed, hour) {
255255
}
256256
```
257257

258+
#### TypeScript
259+
260+
```ts
261+
function minSpeedOnTime(dist: number[], hour: number): number {
262+
if (dist.length > Math.ceil(hour)) return -1;
263+
264+
const check = (speed: number) => {
265+
const n = dist.length;
266+
let time = 0;
267+
268+
for (let i = 0; i < n; i++) {
269+
const t = dist[i] / speed;
270+
time += i === n - 1 ? t : Math.ceil(t);
271+
}
272+
273+
return hour >= time;
274+
};
275+
276+
const max = 10 ** 7;
277+
let [l, r] = [1, max];
278+
279+
while (l <= r) {
280+
const i = (l + r) >> 1;
281+
if (check(i)) r = i - 1;
282+
else l = i + 1;
283+
}
284+
285+
return l <= max ? l : -1;
286+
}
287+
```
288+
258289
<!-- tabs:end -->
259290

260291
<!-- solution:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function minSpeedOnTime(dist: number[], hour: number): number {
2+
if (dist.length > Math.ceil(hour)) return -1;
3+
4+
const check = (speed: number) => {
5+
const n = dist.length;
6+
let time = 0;
7+
8+
for (let i = 0; i < n; i++) {
9+
const t = dist[i] / speed;
10+
time += i === n - 1 ? t : Math.ceil(t);
11+
}
12+
13+
return hour >= time;
14+
};
15+
16+
const max = 10 ** 7;
17+
let [l, r] = [1, max];
18+
19+
while (l <= r) {
20+
const i = (l + r) >> 1;
21+
if (check(i)) r = i - 1;
22+
else l = i + 1;
23+
}
24+
25+
return l <= max ? l : -1;
26+
}

0 commit comments

Comments
 (0)