A car's automatic braking system has three readings: the current speed of the car (in m/s), the distance to an obstacle ahead (in meters), and max_deceleration, the strongest deceleration the car can achieve (in m/s²). Determine whether the car should start braking immediately to avoid a collision.
A car traveling at speed v with maximum deceleration a needs a minimum stopping distance of 2av2. If the stopping distance is greater than or equal to the distance to the obstacle, the car should brake.
Return True if the car should brake and False otherwise.
Function Signature
def should_brake(speed: int, distance: int, max_deceleration: int) -> bool:
Rules
-
The comparison is inclusive. A stopping distance exactly equal to
distance
means the car should brake.
-
The stopping distance is compared as an exact real number, with no tolerance.
Constraints
-
0 <= speed <= 200
-
1 <= distance <= 10000
-
1 <= max_deceleration <= 20
-
speed
is in m/s,
distance
is in meters, and
max_deceleration
is in m/s².
Examples
Input: speed = 30, distance = 50, max_deceleration = 8
Output: True
The stopping distance is 900 / 16 = 56.25 meters. Since 56.25 is at least 50, the car should brake.
Input: speed = 10, distance = 100, max_deceleration = 5
Output: False
The stopping distance is 100 / 10 = 10 meters, which is less than 100, so the car does not need to brake yet.
Input: speed = 20, distance = 40, max_deceleration = 5
Output: True
The stopping distance is 400 / 10 = 40 meters, exactly the distance to the obstacle. Equality counts as a reason to brake.