본문 바로가기
코딩테스트 파이썬/파이썬 프로그래머스 2단계

롤케이크 자르기

by 세용용용용 2023. 5. 20.

첫번쨰 나의 코드

def solution(topping):
    answer = 0
    for i in range(1,len(topping)):
        slice_first = set(topping[:i])
        slice_second = set(topping[i:])
       
        if len(slice_first) == len(slice_second):
            answer+=1
    print(answer)
    return answer
solution([1, 2, 1, 3, 1, 4, 1, 2])

풀면서도 느낌 이렇게 쉬울리는 없을텐디...(무조건 타임아웃 뜰거같음)

ㅋㅋㅋㅋ 정답!!!!! 물론 이렇게 쉽게 풀릴거라 생각안함

 

두번째 나의 코드

from collections import Counter
def solution(topping):
    answer = 0
    chosu = Counter(topping)
    yoohe = set()
    for i in topping:
        chosu[i] -= 1
        yoohe.add(i)

        if chosu[i] == 0:
            chosu.pop(i)
       
        if len(chosu) == len(yoohe):
            answer+=1
    #print(answer)
    return answer
solution([1, 2, 1, 3, 1, 4, 1, 2])

철수꺼를 하나씩 영히한테 주면서 공평한지 확인!!!!

오오오옹 통과!!!!!!

'코딩테스트 파이썬 > 파이썬 프로그래머스 2단계' 카테고리의 다른 글

최댓값과 최솟값  (0) 2023.05.23
소수 찾기  (0) 2023.05.22
다리를 지나는 트럭  (0) 2023.05.20
숫자 변환하기  (1) 2023.05.17
2개 이하로 다른 비트  (0) 2023.05.16