본문 바로가기
알고리즘/Codility

[Codility] Passing Cars (JavaScript)

by devpine 2025. 6. 14.
반응형

https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/

 

PassingCars coding task - Learn to Code - Codility

Count the number of passing cars on the road.

app.codility.com

 

문제:
도로에 겹치는 차량 쌍 개수를 반환한다.

내 풀이:

function solution(A) {
    let eastCnt = 0;
    let pairs = 0;
    for (const a of A) {
        if (a === 0) {
            eastCnt++;
        } else {
            pairs += eastCnt;
        }
    }
    if (pairs > 1000000000) return -1;
    return pairs;
}

채점 결과: 100%

반응형

댓글