C++ Passing Cars
#include <vector>
long long passingCars(const std::vector<int>& a)
{
long long passing = 0;
long long multiply = 0;
for (int i : a) {
if (i == 0) {
++multiply;
} else if (multiply > 0) {
passing += multiply;
if (passing > 1000000000) {
return -1;
}
}
}
return passing;
}
This counts eastbound cars as it scans, then adds them whenever a westbound car appears.