Car Fleet
Count how many groups of cars reach the target, scanning from the car closest to it.
The problem
Implement Solution.carFleet(target, position, speed). n cars drive along a one-lane road toward the point target. Car i starts at position[i] and drives at speed[i]. A car can never pass the car in front of it: when it catches up, it slows down and the two drive on together as one fleet at the slower speed. A car that catches up exactly at target also joins that fleet. A single car on its own is a fleet too. Return how many fleets arrive at target. For example, with target 10, position [0, 5] and speed [2, 1], both cars reach 10 at time 5, so the answer is 1. Constraints: 1 to 100,000 cars, all positions are different and smaller than target, target is at most 1,000,000, and every speed is from 1 to 1,000,000.
Starter code
class Solution:
def carFleet(self, target, position, speed):
passPublic examples
target = 12, position = [10, 8, 0, 5, 3], speed = [2, 4, 1, 1, 3]gives3target = 10, position = [0, 5], speed = [1, 1]gives2target = 10, position = [0, 5], speed = [2, 1]gives1
Solving it in the practice book needs an active paid plan.
Open it in the practice book