TypeScript Passing Cars
function passingCars(a: number[]): number {
let passingCars = 0;
let multiply = 0;
for (const i of a) {
if (i === 0) {
multiply++;
} else if (multiply > 0) {
passingCars += multiply;
if (passingCars > 1000000000) {
return -1;
}
}
}
return passingCars;
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.