Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function carFleet(target: number, position: number[], speed: number[]): number {
- // we don't have to acutally simulate the whole trip
- // 1. figure out when each car would arrive at the destination if it were the only car
- // 2. find the number of cars that had no cars that started ahead of it arrive at or after it would have
- // a. create an array of arrival times ordered by start position, starting with the highstart starting position
- // b. every time you see a new highest arrival time, increment counter
- const carState = position.map((i, ix) => ({ start: i, arrival: (target-i)/speed[ix] })).sort((a,b) => b.start - a.start);
- let worst = 0;
- let count = 0;
- for(const { arrival } of carState) {
- if (arrival > worst) {
- worst = arrival;
- count++;
- }
- }
- return count;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement