Created
July 4, 2016 18:25
-
-
Save tiagopereira17/8d99fad44ccf3f965db7c34117357c40 to your computer and use it in GitHub Desktop.
Count the number of passing cars on the road.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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