Skip to content

Commit 94c35a1

Browse files
committed
feat: add Car Fleet
1 parent 1980920 commit 94c35a1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ https://neetcode.io/roadmap
2525
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | Medium | [TypeScript](./TypeScript/150.evaluate-reverse-polish-notation.ts) | Stack |
2626
| 22 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | Medium | [TypeScript](./TypeScript/22.generate-parentheses.ts) | Stack |
2727
| 739 | [Daily Temperatures](https://leetcode.com/problems/daily-temperatures/) | Medium | [TypeScript](./TypeScript/739.daily-temperatures.ts) | Stack |
28+
| 853 | [Car Fleet](https://leetcode.com/problems/car-fleet/) | Medium | [TypeScript](./TypeScript/853.car-fleet.ts) | Stack |

TypeScript/853.car-fleet.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
type CarPosition = number;
2+
type CarTime = number;
3+
type CarItem = [CarPosition, CarTime];
4+
5+
function carFleet(target: number, position: number[], speed: number[]): number {
6+
const items: CarItem[] = new Array(position.length);
7+
let result = 0;
8+
let maxTime = 0;
9+
10+
for (let i = 0; i < items.length; i++) {
11+
items[i] = [position[i], (target - position[i]) / speed[i]];
12+
}
13+
items.sort(([p1], [p2]) => p2 - p1);
14+
15+
for (let i = 0; i < items.length; i++) {
16+
if (items[i][1] > maxTime) {
17+
result += 1;
18+
maxTime = items[i][1];
19+
}
20+
}
21+
22+
return result;
23+
}

0 commit comments

Comments
 (0)