ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [프로그래머스/level1/python] 수포자
    Algorithm 2022. 4. 17. 12:34
    from collections import deque
    
    def solution(answers):
        answer = []
        
        a = deque([1,2,3,4,5])
        b = deque([2,1,2,3,2,4,2,5])
        c = deque([3,3,1,1,2,2,4,4,5,5])
        
        scores = [0,0,0]
        
        for i in range(len(answers)):
            if(answers[i] == a[0]):
                scores[0] +=1
            if(answers[i] == b[0]):
                scores[1] +=1
            if(answers[i] == c[0]):
                scores[2] +=1
            
            a.rotate(-1)
            b.rotate(-1)
            c.rotate(-1)
        
        max_ = max(scores)
        if(scores[0] == max_):
            answer.append(1)
        if(scores[1] == max_):
            answer.append(2)
        if(scores[2] == max_):
            answer.append(3)
        
        
        return answer

     

    모든 참가자들의 답안은 같은 패턴이 반복되기 때문에

     

    데크(deque)의 rotate() 를 사용해서 문제를 풀어보았다.

     

    각 참가자 별로 답안의 한 사이클을 데크에 저장해둔 다음

     

    answers의 길이만큼 답안을 대조하고 rotate 한다.

     

    그런 다음 최대값을 가진 참가자 번호를 answer에 append 한다.

    'Algorithm' 카테고리의 다른 글

    [Java] Linked List (연결 리스트)  (0) 2020.12.29
Designed by Tistory.