본문 바로가기

공부하자/Codility

[Codility] Lesson5. PassingCars (C#)

문제: 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

 

 

            int result = 0;
            int zero = 0;

            for (int i = 0; i < A.Length; i++)
            {
                if (A[i] == 1)
                {
                    result = result + zero;
                }
                else
                {
                    zero++;
                }
            }

            if (result < 0 || result > 1000000000)
                result = -1;

            return result;