Skip to content

Instantly share code, notes, and snippets.

@tiagopereira17
Created July 4, 2016 18:25
Show Gist options
  • Save tiagopereira17/8d99fad44ccf3f965db7c34117357c40 to your computer and use it in GitHub Desktop.
Save tiagopereira17/8d99fad44ccf3f965db7c34117357c40 to your computer and use it in GitHub Desktop.
Count the number of passing cars on the road.
public class PassingCars {
public int solution(int[] A) {
// 0 - east, 1 - west
int west = 0;
int passing = 0;
for(int i = A.length - 1; i >= 0; i--) {
if(A[i] == 0) {
passing += west;
if(passing > 1000000000) {
return -1;
}
} else {
west += 1;
}
}
return passing;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment