Friday, May 22, 2015

Prefix Sums - PassingCars

Here is another codility problem solution from the codility lessons (PassingCars-Count the number of passing cars on the road.) due to the copy rights I can't copy the content of the problem here so to view the problem description click here.




class Solution {
	public int solution(int[] A) {
		// write your code in Java SE 8
		boolean isZero = false;
		int zeroCount = 0;
		int pairCount = 0;

		for (int i = 0; i < A.length; i++) {
			if (A[i] == 0) {
				isZero = true;
				zeroCount++;
			} else if (A[i] == 1) {
				if (isZero) {
					pairCount = pairCount + zeroCount;
					if (pairCount > 1000000000) return -1;
				} else {
					continue;
				}
			}

		}


		return pairCount;
	}
}

No comments:

Post a Comment