개발자식

[Codility Lesson4] MaxCounters 본문

Algorithm/Codility

[Codility Lesson4] MaxCounters

밍츠 2022. 6. 30. 15:06

문제 : MaxCounters

 

Test results - Codility

You are given N counters, initially set to 0, and you have two possible operations on them: increase(X) − counter X is increased by 1, max counter − all counters are set to the maximum value of any counter. A non-empty array A of M integers is given. T

app.codility.com

 

You are given N counters, initially set to 0, and you have two possible operations on them:

  • increase(X) − counter X is increased by 1,
  • max counter − all counters are set to the maximum value of any counter.

A non-empty array A of M integers is given. This array represents consecutive operations:

  • if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
  • if A[K] = N + 1 then operation K is max counter.

For example, given integer N = 5 and array A such that:

A[0] = 3 A[1] = 4 A[2] = 4 A[3] = 6 A[4] = 1 A[5] = 4 A[6] = 4

the values of the counters after each consecutive operation will be:

(0, 0, 1, 0, 0) (0, 0, 1, 1, 0) (0, 0, 1, 2, 0) (2, 2, 2, 2, 2) (3, 2, 2, 2, 2) (3, 2, 2, 3, 2) (3, 2, 2, 4, 2)

The goal is to calculate the value of every counter after all operations.

Write a function:

class Solution { public int[] solution(int N, int[] A); }

that, given an integer N and a non-empty array A consisting of M integers, returns a sequence of integers representing the values of the counters.

Result array should be returned as an array of integers.

For example, given:

A[0] = 3 A[1] = 4 A[2] = 4 A[3] = 6 A[4] = 1 A[5] = 4 A[6] = 4

the function should return [3, 2, 2, 4, 2], as explained above.

Write an efficient algorithm for the following assumptions:

  • N and M are integers within the range [1..100,000];
  • each element of array A is an integer within the range [1..N + 1].

 

 

 

 

나의 풀이

1차 시도

def solution(N, A):
    temp = [0]*(N+1)

    for i in range(len(A)):
        if A[i] <= N: 
            temp[A[i]]+=1
        else:
            temp = [max(temp)]*(N+1)
        

    return temp[1:]

- O(N*M) , SCORE :66%

- max() : O(N)


2차 시도

def solution(N, A):
    temp = [0]*(N+1)
    maxnum = 0

    for i in range(len(A)):
        if A[i] <= N: 
            temp[A[i]]+=1
            if maxnum < temp[A[i]]:
                maxnum = temp[A[i]]
        else:
            temp = [maxnum]*(N+1)
        

    return temp[1:]

- O(N + M), SCORE : 88%


3차 시도

- 2차 시도 Performance tests를 참고해서 배열A의 모든 값이 N보다 큰 경우를 따로 처리해주었다.

def solution(N, A):
    temp = [0]*(N+1)
    maxnum = 0

    if min(A) > N:
        return temp[1:]

    for i in range(len(A)):
        if A[i] <= N: 
            temp[A[i]]+=1
            if maxnum < temp[A[i]]:
                maxnum = temp[A[i]]
        else:
            temp = [maxnum]*(N+1)
        

    return temp[1:]

- O(N + M), SCORE : 100%

 

테스트 결과를 안봤다면 풀지 못했을거다....

codility는 극단적인 경우의 수를 꼭 고려해줘야 한다는 것을 몸으로 느끼는 중ㅠㅠ!

 

 

다른 풀이

- N+1 값이 들어왔을 때 배열을 새로 만들어서 갱신해주는 방식이 아니라 변수에 넣고 적용해주는 방식인 것 같다

def solution(N,A):
    savemaximum = 0
    maximum = 0
    counter = [0]*N
    for i in range(len(A)):
        if A[i]<=N:
            if counter[A[i]-1]<savemaximum:
                counter[A[i]-1]=savemaximum
            counter[A[i]-1]+=1
            maximum = max(counter[A[i]-1],maximum)
        else:
            savemaximum = maximum
    for i in range(N):
        if counter[i]<savemaximum:
            counter[i]=savemaximum
    return counter
Comments