[프로그래머스] 롤케이크 자르기

bej_ve·2022년 10월 27일
1

python알고리즘

목록 보기
36/46

<문제>
롤케이크에 올려진 토핑들의 번호를 저장한 정수 배열 topping이 매개변수로 주어질 때, 롤케이크를 공평하게 자르는 방법의 수를 return 하도록 solution 함수를 완성해주세요.

<코드>

from collections import Counter

def solution(topping):
    dic=Counter(topping)
    set_dic=set()
    answer=0
    for i in topping:
        dic[i]-=1
        set_dic.add(i)
        if dic[i]==0:
            dic.pop(i)
        if len(dic)==len(set_dic):
            answer+=1
    
    return answer

<풀이>
counter와 dictionary의 개념을 확실히 아는 것이 중요했다.
collections의 Counter를 사용하게 되면 dictionary의 형태로 담기게 된다.
ex) {'1':2, '2':3, '3':4}

0개의 댓글