본문 바로가기

IT/알고리즘

[코딜리티] - Lesson 5. PassingCars

안녕하세요 남갯입니다.





public class PassingCars {

public int solution(int[] A) {
// write your code in Java SE 8
int max = 1000000000;
int oneCount = 0;
int result = 0;
for (int a : A) {
if (a == 1) {
oneCount++;
}
}
//01011
for (int i : A) {
if (i == 0) {
result += oneCount;
if (result > max) {
return -1;
}
} else if (i == 1) {
oneCount--;
}
}
return result;
}

}


1의 갯수를 구한뒤 0의 순서부터 1의 갯수를 줄줄이 뺀 갯수의 전체 합을 구하는 로직


https://app.codility.com/demo/results/training9EDUB4-CMU/