Java Passing Cars
public class Solution {
public static int passingCars(int[] a) {
long passingCars = 0;
int multiply = 0;
for (int i : a) {
if (i == 0) {
multiply++;
} else if (multiply > 0) {
passingCars += multiply;
if (passingCars > 1000000000) {
return -1;
}
}
}
return (int) passingCars;
}
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.