본문 바로가기

⏳ 알고리즘/python 알고리즘 문제 풀이

프로그래머스 - LV2. 롤케이크 자르기

문제

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

 

프로그래머스

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

programmers.co.kr

 

코드

def solution(topping):
    answer = 0
    
    for i in range(1, len(topping)):
        if len(set(topping[:i])) == len(set(topping[i:])):
            answer += 1
        
    return answer

시간 초과!

 

from collections import Counter

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