본문 바로가기

공부하자/Codility

[Codility] Lesson10. Flags (C#)

문제: app.codility.com/programmers/lessons/10-prime_and_composite_numbers/flags/

 

Flags coding task - Learn to Code - Codility

Find the maximum number of flags that can be set on mountain peaks.

app.codility.com

 

 

 

결과: 20% 뭐지 이건..

 

            if (A.Length < 3) return 0;
            List<int> peek = new List<int>();
            for (int i = 1; i < A.Length - 1; i++)
            {
                if (A[i] > A[i - 1] && A[i] > A[i + 1])
                {
                    peek.Add(A[i]);
                }
            }

            if (peek.Count < 1) return 0;

            int before = peek[0];
            int count = 1;
            for (int i = 1; i < peek.Count; i++)
            {
                if (peek[i] != before)
                    count++;
                before = peek[i];
            }
            return count;