본문 바로가기

공부하자/Codility

[Codility] Lesson5. MinAvgTwoSlice (C#)

문제: app.codility.com/programmers/lessons/5-prefix_sums/min_avg_two_slice/

 

MinAvgTwoSlice coding task - Learn to Code - Codility

Find the minimal average of any slice containing at least two elements.

app.codility.com

 

 

            double minAvg = (A[0] + A[1]) / 2.0;
            int result = 0;

            for (int i = 2; i < A.Length; i++)
            {
                double avg = (A[i] + A[i - 1] + A[i - 2]) / 3.0;
                if (minAvg > avg)
                {
                    minAvg = avg;
                    result = i - 2;
                }

                avg = (A[i] + A[i - 1]) / 2.0;
                if (minAvg > avg)
                {
                    minAvg = avg;
                    result = i - 1;
                }
            }

            return result;