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

 

 

  • 배열 A가 주어질 때, 연속되는 요소는 도로의 연속되는 자동차를 표현한 것이다.
  • 배열 A에는 0(동쪽) 또는 1(서쪽)만 포함된다. 
  • (P,Q) 동쪽으로 이동하는 차들이 서쪽으로 가는 자동차를 몇 번 마주치는지 구하기
  • 지나가는 자동차의 쌍의 개수가 1,000,000,000개를 초과하면 -1 출력
  • O(N)
def solution(A):
    cnt = 0
    total = 0
    for idx in range(len(A)):
        if idx == 1 and cnt == 0:
            continue
        if A[idx] == 0:
            cnt = cnt + 1
        else:
            total = total + cnt

    if total > 1000000000:
        return -1
    return total

+ Recent posts