코딩테스트/프로그래머스

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

박소민 2024. 11. 17. 23:37
롤케이크 자르기
 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

  • 내 풀이
    • Counter 사용하고
    • 시간초과나서 최대한 연산 횟수 줄이도록 코드 수정
      • 매번 count로 계산하지 않고 dict_cnt[t]<=0이 되는지 체크하며 진행
from collections import Counter

def solution(topping):
    answer = 0
    dict_cnt=Counter(topping)
    n=len(set(topping))
    
    m=0
    set1=set()
    for t in topping:
        if t not in set1:
            m+=1
            set1.add(t)
        dict_cnt[t]-=1
        
        if dict_cnt[t]<=0:
            n-=1
        
        if n==m:
            answer+=1
        
        
    return answer