본문 바로가기

공부하자/Codility

[Codility] Lesson4. MaxCounters (C#)

문제: app.codility.com/programmers/lessons/4-counting_elements/max_counters/

 

MaxCounters coding task - Learn to Code - Codility

Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.

app.codility.com

 

77% (다시 풀어야 함)

            int[] result = new int[N];
            int max = 0;

            for (int i = 0; i < A.Length; i++)
            {
                int position = A[i] - 1;

                if (A[i] >= 1 && A[i] <= N)
                {
                    result[position] = result[position] + 1;

                    if (max < result[position])
                        max = result[position];
                }
                else if (A[i] > N)
                {
                    for (int j = 0; j < result.Length; j++)
                        result[j] = max;
                }
            }

            return result;