Skip to content

Commit dd6291d

Browse files
authored
Update Solution.ts
1 parent 5e248e5 commit dd6291d

File tree

1 file changed

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

1 file changed

+20
-21
lines changed
Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
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);
2+
if (dist.length > Math.ceil(hour)) {
3+
return -1;
4+
}
5+
const n = dist.length;
6+
const m = 10 ** 7;
7+
const check = (v: number): boolean => {
8+
let s = 0;
9+
for (let i = 0; i < n; ++i) {
10+
const t = dist[i] / v;
11+
s += i === n - 1 ? t : Math.ceil(t);
1112
}
12-
13-
return hour >= time;
13+
return s <= hour;
1414
};
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;
15+
let [l, r] = [1, m + 1];
16+
while (l < r) {
17+
const mid = (l + r) >> 1;
18+
if (check(mid)) {
19+
r = mid;
20+
} else {
21+
l = mid + 1;
22+
}
2323
}
24-
25-
return l <= max ? l : -1;
24+
return l > m ? -1 : l;
2625
}

0 commit comments

Comments
 (0)