동캄의 코딩도장

프로그래머스 level2 [롤케이크 자르기] 파이썬 본문

코테/프로그래머스

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

동 캄 2023. 4. 6. 00:12

https://school.programmers.co.kr/learn/courses/30/lessons/132265

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

from collections import defaultdict
def solution(topping):
    answer = 0
    a=defaultdict(int) # 형 롤케잌
    b=defaultdict(int) # 동생 롤케잌
    for t in topping: # 형이 롤케잌 다 가져감
        a[t]+=1
    for t in topping: #제일 끝부터 하나씩 동생에게 떼어줌
        a[t]-=1
        if a[t]==0:
            a.pop(t)
        b[t]+=1
        if len(a)==len(b): #만약 토핑 개수가 같다면
            answer+=1
    
    return answer

default dict를 이용하여 해결하였다.

먼저 한쪽으로 케이크를 다 몰아준 뒤, 한 토핑씩 반대쪽으로 보내며 체크한다.