상세 컨텐츠

본문 제목

[Lesson 5] Prefix Sums - PassingCars

Codility Lessons 풀이

by 발발개발 2025. 1. 3. 10:45

본문

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

class Solution {
    public int solution(int[] A) {
        int sum = 0;
        int result = 0;

        for (int i = A.length - 1; i >= 0; i--) {
            if (A[i] == 1) {
                sum += 1;
            } else {
                result += sum;
            }

            if (result > 1000000000) {
                return -1;
            }
        }

        return result;
    }
}

관련글 더보기

댓글 영역